Perl-Sensitive Sunglasses PerlMonks

### Re: Sorting a hash of hashes

by diddy_perl (Novice)
 on May 15, 2012 at 08:42 UTC ( #970582=note: print w/replies, xml ) Need Help??

in reply to Sorting a hash of hashes

Try this

```## Fort test1

## sort in descending order by value1, select first
@values1 = map { \$_, \$HoH{\$_}{'Test1'}{'value1'} }
sort { \$HoH{\$b}{'Test1'}{'value1'} <=> \$HoH{\$a}{'Test1'}{'value1'}
+ } keys %HoH;

## sort in descending order by value2, select first
@values2 = map { \$_, \$HoH{\$_}{'Test1'}{'value2'} }
sort { \$HoH{\$b}{'Test1'}{'value2'} <=> \$HoH{\$a}{'Test1'}{'value2'}
+ } keys %HoH;

print "Fort Test1\n";
print "  ", \$values1[0], " has value1=", \$values1[1], "\n";
print "  ", \$values2[0], " has value2=", \$values2[1], "\n";

## For test2

## sort in descending order by value1, select first
@values1 = map { \$_, \$HoH{\$_}{'Test2'}{'value1'} }
sort { \$HoH{\$b}{'Test2'}{'value1'} <=> \$HoH{\$a}{'Test2'}{'value1'}
+ } keys %HoH;

## sort in descending order by value2, select first
@values2 = map { \$_, \$HoH{\$_}{'Test2'}{'value2'} }
sort { \$HoH{\$b}{'Test2'}{'value2'} <=> \$HoH{\$a}{'Test2'}{'value2'}
+ } keys %HoH;

print "Fort Test2\n";
print "  ", \$values1[0], " has value1=", \$values1[1], "\n";
print "  ", \$values2[0], " has value2=", \$values2[1], "\n";

Enjoy!

Create A New User
Node Status?
node history
Node Type: note [id://970582]
help
Chatterbox?
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-07-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (423 votes). Check out past polls.

Notices?