Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Twice the pleasure of sorting a hash

by broquaint (Abbot)
on Apr 26, 2004 at 09:52 UTC ( #348121=note: print w/replies, xml ) Need Help??

in reply to Twice the pleasure of sorting a hash

A double comparison should do it e.g
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
So firstly we sort by value in descending order then optionally sort by key value.


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
    That works perfectly, you're the best!! Thanks so much for your help broquaint!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://348121]
[erix]: I recognise the makings of a fine argument
[LanX]: lanx wonders ... how likely is it to talk >95% BS without intention?
[erix]: "gigantic amounts of data" is also not SQLite (imho)
talexb wonders why sqlite2 was deprecated in favour of sqlite3.
[erix]: looks like a fork, rather, no?
LanX /me /me
[erix]: /hehehe
[marto]: LanX yesterday I found out about Gish gallop tactic

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2017-07-28 15:35 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.