sub split_sort { my ($in_a, $in_b, $split, $sort_type) = @_; $split = qr($split); my ($numa1, $numa2) = split(/$split/, $in_a); my ($numb1, $numb2) = split(/$split/, $in_b); if ($sort_type =~ /^num/) { $numa1 <=> $numb1 || $numa2 <=> $numb2 } elsif ($sort_type =~ /^(alpha|letter)/) { $numa1 cmp $numb1 || $numa2 cmp $numb2 } } #### my @numbers = qw(1:2 1:02 3:4 5:78 50:89 10:5); my @sorted = sort @numbers; # The list as it is written can not be sorted as numbers. # returns # [ # '10:5', # '1:02', # '1:2', # '3:4', # '50:89', # '5:78' # ] my @split_sorted = sort { split_sort($a, $b, ':', 'number') } @numbers; # returns # [ # '1:2', # '1:02', # '3:4', # '5:78', # '10:5', # '50:89' # ];