in reply to sorting a hash by its values, date

I have stolen ikegama's idea of using map along with aquarium's idea of reordering the date so it sorts easily and come up with the following little snippet. The date is re-ordered in the map block and munged together with the key to make it easy to sort the entire thing as an array. once sorted it is easy to reconstruct the data as you like.

This does depend on the day, date and year being 2,2,4 digits, if they are not then a little more needs adding in the map block to fix this

cheers.

#!/usr/local/bin/perl -w use strict; my %h = ( key1 => "10/14/2003", key2 => "11/23/2001", key3 => "12/23/2001", key4 => "12/22/2001", key5 => "02/21/1984", key6 => "08/13/1969", key7 => "09/11/1973", key8 => "09/30/2000" ); my @bydate = map { my @tmp = split /\//, $h{$_}; $tmp[2].":".$tmp[0].":".$tmp[1].":".$_ } keys(%h); foreach (sort @bydate) { my ($yr, $mn, $dy, $key) = split /:/; print "$mn/$dy/$yr = $key\n"; }

update
I have been playing with pack/unpack and thought this was a nice place to use it. So here is a more efficient version
#!/usr/local/bin/perl -w use strict; my %h = ( key1 => "10/14/2003", key2 => "11/23/2001", key3 => "12/23/2001", key4 => "12/22/2001", key5 => "02/21/1984", key6 => "08/13/1969", key7 => "09/11/1973", key8 => "09/30/2000" ); my @bydate = map { my @tmp = split /\//, $h{$_}; pack 'A4A2A2A*', $tmp[2], $tmp[0], $tmp[1], $_; } keys(%h); foreach (sort @bydate) { print "@{[unpack ('A4A2A2A*',$_)]}\n"; }