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

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.