ハッシュのキーを値でソートする

back
----
%hash = ( key1 => rand,
          key2 => rand,
          key3 => rand,
          key4 => rand,
          key5 => rand );

@keys = keys %hash;
foreach (@keys) { print "$_: $hash{$_}\n" }
print "----\n";

@sorted_keys = map { $_->[1] }
               sort { $b->[0] <=> $a->[0] }
               map { [$hash{$_}, $_] }
               keys %hash;

foreach (@sorted_keys) { print "$_: $hash{$_}\n" }
----

結果
key5: 0.910997735930398
key2: 0.90412388145657
key4: 0.191655535108062
key1: 0.594831374780373
key3: 0.992515315805701
----
key3: 0.992515315805701
key5: 0.910997735930398
key2: 0.90412388145657
key1: 0.594831374780373
key4: 0.191655535108062


まぁ、シュウォーツ変換(Schwartzian Transform)してるだけなんだけど。

back