in reply to sorting an array with an array

my %order; @order{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = (1..12); ... my @sorted_filenames = sort { $order{substr($a,0,3)} <=> $order{substr($b,0,3)} } @unsorted_filenames;
This'll be ok up to 100 filenames or so. If you have more, you'll probably want to cache the substr-naming mapping using a Schwartzian Transform or other device.

Or, you could even go for a merge sort, which will scale for 1000's of filenames:

my %piles; for (@unsorted_filenames) { push @{$piles{substr($_, 0, 3)}), $_; } my @result; for (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)) { push @result, sort @{$piles{$_}}; delete $piles{$_}; } die if keys %piles; # unexpected prefix!

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
(tye)Re2: sorting an array with an array
by tye (Sage) on Oct 01, 2002 at 16:08 UTC
    1 file for each day of the year

    Your first case assumes one file per month (well, it doesn't sort within each month, which was requested). Your second case doesn't deal with the possibility of a lack of leading zeros in days of the month.

            - tye (a leading zero himself)