I like much of what mirod has to say above. Let me just offer YAWTDI, which technique may prove useful elsewhere. You want to sort by year first, and if the year is the same between two files, by month. Neat trick:
my @sorted = sort by_year_and_month @files; sub by_year_and_month { year($a) <=> year($b) or month($a) <=>month($b); } sub month { substr shift, 0, 2; #used to say '0, 1' but chipmunk caught the mi +stake } sub year substr shift, 2, 2; #used to be 2, 3 but see above! }
How it works: if the first <=> comes out 'even' (==0), the second one kicks in. Otherwise, the value of the first comparison is used.
This slab o' code is not efficient (does the month and year transforms each time ... ugh!). If I were thinking, I'd put a map in here somewhere, but I'll leave that up to you.
Props to Effective Perl Programming, from whence I picked up the disjunctive sort.
Philosophy can be made out of anything. Or less -- Jerry A. Fodor
In reply to Re: sorting by month within year
by arturo
in thread sorting by month within year
by Prince99
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |