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.
|
---|