in reply to How can I do a numeric sort on a substring?

I wouldn't worry about whether this is one line of code or not.
A simple way is shown below.
$a and $b are special variables used by sort.
Here the numeric value at the end of string is used as the primary thing to sort upon.
If the numeric values are equal, then string comparison is used as a "tie breaker".

This will work fine performance wise for say 100 things.
If there are 1,000 things, start to think about something more complex.
If there are 10,000 things definitely think about more complex idioms if you do this "often".

I would recommend keeping things simple unless there is an obvious performance reason that requires the sort to be faster.

use strict; use warnings; my @array = qw(blah-13 yup-09 weird-2 stranger-1 strange-1); print "Input array:\n"; print "$_\n" for @array; @array = sort { my ($atext,$anum) = $a =~ /(\w+)-(\d+)/; my ($btext,$bnum) = $b =~ /(\w+)-(\d+)/; $anum <=> $bnum or $atext cmp $btext }@array; print "\nOutput array:\n"; print "$_\n" for @array; __END__ Input array: blah-13 yup-09 weird-2 stranger-1 strange-1 Output array: strange-1 stranger-1 weird-2 yup-09 blah-13
An Update: I re-iterate my suggestion about the 10,000 items level. No, I don't present benchmarks of my own, but in my experience, that is where a very noticeable impact of say the ST will become apparent. If your program is interacting with a single user, even quite stunning performance increases might make little difference. A difference of say 20 ms is basically undetectable at the user UI level. If you are writing server level code then performance matters a lot more because it affects the number of users that can be serviced by your machine.

Another update: A difference of 50 ms (1/20th of a second) will be noticeable by the user in things like audio playback of multiple files. Less than that doesn't make much difference. If your sort takes even 3ms which is a LONG time by modern computer standards, so what? Unless this a server process, I wouldn't worry about it.