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 ).
|
|---|