in reply to sorting keys in hash

Using split, a hash for month sort order look-ups and a ST seems simplest.

$ perl -Mstrict -Mwarnings -E ' my %mthSO = do { my $order; map { $_, ++ $order } qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec }; }; say for map { $_->[ 0 ] } sort { $a->[ 2 ] <=> $b->[ 2 ] || $mthSO{ $a->[ 1 ] } <=> $mthSO{ $b->[ 1 ] } } map { [ $_, split m{ (?<= [a-z] ) (?= \d ) }x ] } qw{ May14 Dec13 Mar14 Oct12 Apr14 };' Oct12 Dec13 Mar14 Apr14 May14 $

I hope this is helpful.

Update: Corrected lame-brain wrong comparator for month, thanks AnomalousMonk ++

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: sorting keys in hash
by AnomalousMonk (Archbishop) on May 23, 2014 at 23:27 UTC
    my %mthSO = do { my $order; map { $_, ++ $order } qw{ ... }; };
    ...
    $mthSO{ $a->[ 1 ] } cmp $mthSO{ $b->[ 1 ] }

    Since month names are map-ped to numeric values, shouldn't the month name value comparison be numeric  <=> rather than lexicographic? Adding the dates  Jan14 Dec14 to the test set shows the problem.

      Yep, corrected, lamentable senior moment :-(

      Cheers,

      JohnGG