in reply to Twice the pleasure of sorting a hash
A double comparison should do it e.g
So firstly we sort by value in descending order then optionally sort by key value.my %h = ( rock => 3, candle => 25, bug => 3, rain => 12, dust => 17, spider => 12, ); for( sort { $h{$b} <=> $h{$a} || $a cmp $b } keys %h ) { print "$_ => $h{$_}\n"; } __output__ candle => 25 dust => 17 rain => 12 spider => 12 bug => 3 rock => 3
HTH
_________
broquaint
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Twice the pleasure of sorting a hash
by coldfingertips (Pilgrim) on Apr 26, 2004 at 09:55 UTC |
In Section
Seekers of Perl Wisdom