... the numbers presented are zero-padded integers [with three digits each], so the comparison is equivalent.

Aye, there's the rub. The OPer only presents three examples and, in general, about 35% of the info needed to build some robust code. Numeric and string comparison are the same until someone sez "Oh, by the way..."

It's just a guess, but I'd be inclined to fortify myself on the numeric side based on the presence of the word "number" in the OP: confronted with real-world data (whatever that may be), a numeric comparison seems more likely to continue to work.

I was thinking of something along the lines of Marshall:

>perl -wMstrict -le "my @names = qw(FLY_ABC123_01.jpg PG_ABC123_03 SPL_ABC123_02.jpg FOO_ABC123_011 SPL_ABC123_021 SPL_ABC123_021.jpg); sub sort_123 { return map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, m{ .* _ (\d+) }xms ], @_ ; } my @sorted = sort_123(@names); print qq{'$_'} for @sorted; " 'FLY_ABC123_01.jpg' 'SPL_ABC123_02.jpg' 'PG_ABC123_03' 'FOO_ABC123_011' 'SPL_ABC123_021' 'SPL_ABC123_021.jpg'

In reply to Re^4: Sorting filenames with custom function by AnomalousMonk
in thread Sorting filenames with custom function by Anonymous Monk

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.