Took me a while to come full circle to this, but was trying crazy stuff with splice after moving keys to array! ABORT!!!

So, I realized that once months were translated to numbers, concatenating the day and month in reverse would be easily sortable:

Jul11 = 1107 Jun11 = 1106 Aug13 = 1308 May14 = 1405 etc...

Update

Looked up s///e in the Camel Book and was able to shorten the sortKeys function from:
sub sortKeys { my ( $l_mo, $l_yr ) = $a =~ /(\D+)(\d+)/; my ( $r_mo, $r_yr ) = $b =~ /(\D+)(\d+)/; my $left = $l_yr . $months->{$l_mo}; my $right = $r_yr . $months->{$r_mo}; return ( $left cmp $right ); }
to:
sub sortKeys { ( my $left = $a ) =~ s/(\D+)(\d+)/$2 . $months->{$1}/e; ( my $right = $b ) =~ s/(\D+)(\d+)/$2 . $months->{$1}/e; return ( $left cmp $right ); }

Modified code below:

#!/usr/bin/perl # http://www.perlmonks.org/?node=1087227 use strict; use warnings; my $months = { "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" +=> "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" +=> "11", "Dec" => "12", }; my $data = { "Jul11" => 2, "Dec13" => 2, "Jan14" => 2, "Aug13" => 2, "Jun11" => 2, "Apr13" => 2, "Mar12" => 2, "May14" => 2, }; # using regular sort print "before:\n"; foreach my $key (sort keys %$data) { print "Key - $key and b value - $data->{$key}\n"; } # using sortKeys sort print "after\n"; foreach my $key ( sort { sortKeys( $a, $b ) } keys %$data ) { print "Key - $key and b value - $data->{$key}\n"; } sub sortKeys { ( my $left = $a ) =~ s/(\D+)(\d+)/$2 . $months->{$1}/e; ( my $right = $b ) =~ s/(\D+)(\d+)/$2 . $months->{$1}/e; return ( $left cmp $right ); } __output__ $ ./node_id_1087227_hashkey_sort.pl before: Key - Apr13 and b value - 2 Key - Aug13 and b value - 2 Key - Dec13 and b value - 2 Key - Jan14 and b value - 2 Key - Jul11 and b value - 2 Key - Jun11 and b value - 2 Key - Mar12 and b value - 2 Key - May14 and b value - 2 after Key - Jun11 and b value - 2 Key - Jul11 and b value - 2 Key - Mar12 and b value - 2 Key - Apr13 and b value - 2 Key - Aug13 and b value - 2 Key - Dec13 and b value - 2 Key - Jan14 and b value - 2 Key - May14 and b value - 2

In reply to Re: sorting keys in hash by dbuckhal
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.