Use a sort sub-routine. Don't know how you want to sort (you give no example :), but here's a start:
# sample data my @filenames = '20001212_20011212_text.asp', '20021212_20031212_text.asp', '20031212_20041212_image.asp', '20011212_20021212_image.asp'); # loop for (sort { sort_me(); } @filenames) { # whatever print "$_\n"; } sub sort_me { # grab info from special vars $a and $b my ($a_date1,$a_date2) = split '_', $a; my ($b_date1,$b_date2) = split '_', $b; # then do your sort - eg, to sort by date1 then date2 # (in the date format I have used above) ($a_date1 <=> $b_date1) || ($a_date2 <=> $b_date2); }

$a and $b are special vars used within a sort. Read this for more info.

Note, this is a rough demo - not gonna test it unless you supply actual data and comparisons... but should give you an idea where to start.

cLive ;-)

Update - just realised you don't need to know type, so have amended code accordingly. later one, removed semi-colon that didn't affect outcome, but still looked out of place...


In reply to Re: How do I sort by certain parts of a filename? by cLive ;-)
in thread How do I sort by certain parts of a filename? by wstarrs

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.