Ok, I'm acting under the assumption that the leading "F" in your filenames is always going to be "F". If not, this might not do everything you need. Here goes:

use strict; use warnings; my @arr=( qw/ F1.jpg F10.jpg F11.jpg F12.jpg F1a.jpg F1b.jpg F2.jpg F3.jpg F4.jpg F5.jpg F6.jpg /); print "$_\n" for sort { my( $aye ) = $a =~ m/(\d+)/; my( $bee ) = $b =~ m/(\d+)/; $aye <=> $bee || $a cmp $b } @arr;

Here's how it works: Isolate the digits in each comparison, and do a numeric comparison just on the digits. If the numeric comparison equates to equality, the logical OR short circuit operator falls through to the string comparison. So you get numeric first, and asciibetical order second. I hope that's close to what you were looking for.

If the dataset is large and you intend on running the sort more than once, you might benefit from a Schwartzian Transform, but my guess is that you really don't need to be that concerned with efficiency.

For more info on sorting, read sort.

Update:I just noticed you want F1.jpg to appear after F1a.jpg. That's an unusual sort order. Are you sure you want that? Anyway, I've given at least enough of a building-block example upon which you can construct even more complex sort routines to satisfy whatever definition of "sorted" you wish to come up with. :) Enjoy!.


Dave


In reply to Re: Sort Array by davido
in thread Sort Array 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.