If you're going to go to the trouble of two  s/// operations, you might as well go all the way to a GRT/decorate-sort-undecorate solution; then you can at least claim to be getting the maximum possible performance from the Perl sort built-in.

c:\@Work\Perl\monks>perl -wMstrict -le "my %data = qw( Dec02 2 Jan02 3 May05 4 Dec14 8 Dec01 5 Jan14 7 Apr05 6 Feb14 9 May15 10 ); ;; use constant Y_WID => 2; use constant M_WID => 3; my %mon_n = qw( Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6 Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12 ); ;; my @sorted_dates = map unpack(qq{x${\Y_WID} x${\M_WID} a*}, $_), sort map sprintf('%*s%*s%s', Y_WID, $_->[1], M_WID, $mon_n{$_->[0]}, $_- +>[2]), map [ m{ \A ([[:alpha:]]{${\M_WID}}) (\d{${\Y_WID}}) \z }xms, $_ ], keys %data ; ;; print qq{Key - $_ and b value - $data{$_}} for @sorted_dates; " Key - Dec01 and b value - 5 Key - Jan02 and b value - 3 Key - Dec02 and b value - 2 Key - Apr05 and b value - 6 Key - May05 and b value - 4 Key - Jan14 and b value - 7 Key - Feb14 and b value - 9 Key - Dec14 and b value - 8 Key - May15 and b value - 10

Update: See A Fresh Look at Efficient Perl Sorting by Guttman and Rosler (the G and R of GRT). Also: It seems to me now that all the regex sub-string extraction and sprintf-ing would have been much neater, clearer and more maintainable if encapsulated into a separate function. Oh, well...


In reply to Re^2: sorting keys in hash by AnomalousMonk
in thread sorting keys in hash by Perlseeker_1

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.