in reply to Sorting an hash of an array by a value in the array
You cannot actually "sort a hash" per se, but you can create an array of the keys in the required order and then use that to iterate the hash:
%h = ( a=>[3,1..4], b=>[2,1..4], c=>[0..4] );; @sortedKeys = sort{ $h{ $a }[0] <=> $h{ $b }[0] } keys %h;; print "$_ : @{ $h{ $_ } }" for @sortedKeys;; c : 0 1 2 3 4 b : 2 1 2 3 4 a : 3 1 2 3 4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting an hash of an array by a value in the array
by Anonymous Monk on Jan 12, 2012 at 16:10 UTC |