| Developer's Daily | Perl Q&A Center |
| main | java | perl | unix | dev directory | web log |
Question: How do I sort a hash by the hash value?
Answer:
First, sorting a hash by the hash key
Sorting the output of a hash by the hash key is a pretty well-known recipe. It's covered in another Q&A article titled "How to sort a hash by the hash key".
Sorting a hash by the hash valueSorting a hash by the hash value is a bit more difficult than sorting the hash by the key, but it's not too bad. It just requires a small "helper" function.
This is easiest to demonstrate by example. Suppose we have a class of five students. Rather than give them names, we'll call them student1, student2, etc. Suppose these students just took a test, and we stored their grades in a hash (called associative arrays prior to the release of Perl 5) named grades.
The hash definition might look like this:
%grades = ( student1 => 90, student2 => 75, student3 => 96, student4 => 55, student5 => 76, );If you're familiar with hashes, you know that the student names are the keys, and the test scores are the hash values.
The key to sorting a hash by value is the function you create to help the sort command perform it's function. Following the format defined by the creators of Perl, you create a function I call a helper function that tells Perl how to sort the list it's about to receive. In the case of the program you're about to see, I've created two helper functions named hashValueDescendingNum (sort by hash value in descending numeric order) and hashValueAscendingNum (sort by hash value in ascending numeric order).
Here's a program that prints the contents of the grades hash, sorted numerically by the hash value:
#!/usr/bin/perl -w #----------------------------------------------------------------------# # printHashByValue.pl # # # # Copyright 1998 DevDaily Interactive, Inc. All Rights Reserved. # #----------------------------------------------------------------------# #----------------------------------------------------------------------# # FUNCTION: hashValueAscendingNum # # # # PURPOSE: Help sort a hash by the hash 'value', not the 'key'. # # Values are returned in ascending numeric order (lowest # # to highest). # #----------------------------------------------------------------------# sub hashValueAscendingNum { $grades{$a} <=> $grades{$b}; } #----------------------------------------------------------------------# # FUNCTION: hashValueDescendingNum # # # # PURPOSE: Help sort a hash by the hash 'value', not the 'key'. # # Values are returned in descending numeric order # # (highest to lowest). # #----------------------------------------------------------------------# sub hashValueDescendingNum { $grades{$b} <=> $grades{$a}; } %grades = ( student1 => 90, student2 => 75, student3 => 96, student4 => 55, student5 => 76, ); print "\n\tGRADES IN ASCENDING NUMERIC ORDER:\n"; foreach $key (sort hashValueAscendingNum (keys(%grades))) { print "\t\t$grades{$key} \t\t $key\n"; } print "\n\tGRADES IN DESCENDING NUMERIC ORDER:\n"; foreach $key (sort hashValueDescendingNum (keys(%grades))) { print "\t\t$grades{$key} \t\t $key\n"; }Although this demo program is fairly lengthy, you can see at the bottom of the code where the student grades are printed in ascending and descending numeric value.
The output of the program looks like this:
GRADES IN ASCENDING NUMERIC ORDER: 55 student4 75 student2 76 student5 90 student1 96 student3 GRADES IN DESCENDING NUMERIC ORDER: 96 student3 90 student1 76 student5 75 student2 55 student4
Thanks for joining us. I hope you enjoyed this article! Three final thoughts before you go:
If you're interested in downloading the code for your own programming purposes, you're welcome to click here to download the code.
As mentioned earlier, there is a companion Q&A article named "How to sort a hash by the hash key".
Good luck, and let us know if you have any problems.