in reply to Sorting a hash of arrays based on a particular array element.

Supply the keys of %hash to sort and then use $hash{ $a | $b }[0] to access your sort field:

%hash = ( 1 => [ 51, 'a2'], 2 => [42, 'a1'] );; # NOTE: ^______ parens not curlies _________^ print "$_ => @{$hash{$_}}" for sort{ $hash{$a}[0] <=> $hash{$b}[0] } keys %hash;; 2 => 42 a1 1 => 51 a2

Realise that you will need sort the hash each time you want to iterate it in sorted order (or use one of the sorted hash implementations on CPAN ).


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.