http://www.perlmonks.org?node_id=692835


in reply to how to sort HoH according to value

sovixi,

The key thing to realize here is that a hash doesn't have any notion of order. The most straightforward solution is to copy the information you want from the hash into a list, which is ordered, then sort the list. For example:

# Convert HoH into a list of arrayrefs: [value, key1, key2, ...] my @list; while (my($k1,$v1) = each(%HoH)) { while (my($k2,$v2) = each(%$v1)) { push @list, [$v2, $k1, $k2]; } } # Sort the list by its first element @list = sort { $a->[0] <=> $b->[0] } @list; # Now print the list foreach my $elt (@list) { print "@$elt\n"; }