Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: how to put hash key and value in order

by frozenwithjoy (Priest)
on Dec 20, 2012 at 03:01 UTC ( #1009677=note: print w/replies, xml ) Need Help??

in reply to how to put hash key and value in order

To sort by ascending value:

say "$_: $coins{$_}" for sort { $coins{$a} <=> $coins{$b} } keys %coins;

To sort by descending value:

say "$_: $coins{$_}" for sort { $coins{$b} <=> $coins{$a} } keys %coins;

To sort by ascending value followed by descending key (not really applicable for this coin example, but good to know):

say "$_: $coins{$_}" for sort { ( $coins{$a} <=> $coins{$b} ) || ( $a cmp $b ) } keys %coins;

Note: Be sure to use <=> for numerical sorts and cmp for alphabetical.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009677]
[Discipulus]: if i can suggest La Reine Margot too is a great book and obviously The Count of Monte Cristo that is a chef-d'œuvre
[Discipulus]: The red sphinx is about Rechilieu, absolutely thrilling
[erix]: thanks, Discipulus - I got them lined up at gutenberg :)
[erix]: I red the musketeers and Monte Cristo but both a very long time ago (many decades ago)
[erix]: *read
[Discipulus]: dunno if a young people can well understand the deep of Montecristo

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-05-24 07:25 GMT
Find Nodes?
    Voting Booth?