in reply to sorting an array with an array
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.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;
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 |