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.


In reply to Re: How can I do a numeric sort on a substring? by Marshall
in thread How can I do a numeric sort on a substring? by misterperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.