haoess has asked for the wisdom of the Perl Monks concerning the following question:
Like in Perl 5 you can print your hash sorted by its values:
Output (it works with my latest Rakudo checkout):my %nums = ( four => 4, one => 1, three => 3, two => 2, ); for %nums.keys.sort: { %nums{$^a} <=> %nums{$^b} } -> $key { say "$key {%nums{$key}}"; }
one 1 two 2 three 3 four 4
Maybe there's a more comfortable (aka shorter) way to get this, without writing %nums three times on the same line?
Thanks, Frank
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: [Perl6] Sort a hash by value
by moritz (Cardinal) on Dec 09, 2008 at 19:13 UTC | |
by haoess (Curate) on Dec 12, 2008 at 19:14 UTC |
Back to
Seekers of Perl Wisdom