Home: Perl Programming Help: Frequently Asked Questions:
How Do I Sort A Hash by Either Value Or Key



sleuth
Enthusiast

Dec 12, 2000, 2:43 PM


Views: 7343
How Do I Sort A Hash by Either Value Or Key

Here is a good example of how to sort a hashes records by value, please pay attention to the 3rd and 4th posts by myself and rGeoffrey, the first two of mine are BAD coding examples, DO NOT USE THEM.

To sort a hash by key, is much more simple,

foreach $key (sort keys %hash){
print "sorted key: $key, value: $hash{$key}";
}

An excellent article about hashes written by Jasmine the administrator of this site can be found here.

Sleuth