in reply to sorting a hash by its values, date
sort's second argument is a list. %H, as a list, would look like ('key2', 'value2', 'key1', 'value1', 'key3', 'value3'). That means that by_date would be provided not just with the values, but with the keys as well.
Here's a solution:
sub by_date { my ($monA, $monB, $dayA, $dayB, $yearA, $yearB); ($monA, $dayA, $yearA) = split(/\//, $a->[1]); # <-- slight change ($monB, $dayB, $yearB) = split(/\//, $b->[1]); # <-- slight change $yearA <=> $yearB or $monA <=> $monB or $dayA <=> $dayB } @H = map { [ $_, $H{$_} ] } keys(%H); foreach $k (sort by_date @H)) { # $k->[0] is the key. # $k->[1] is the value. ... }
|
|---|