Perl hash sorting FAQ: How do I sort a Perl hash by the hash value?
Before getting into this article, you if just need to sort a Perl hash by the hash key, this 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 Perl hash by the hash value
Sorting a Perl hash by the hash value is a bit more difficult than sorting 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).
A complete Perl hash sort sample program
Here's a complete Perl hash sort sample program that prints the contents of the grades hash, sorted numerically by the hash value:
#!/usr/bin/perl -w #----------------------------------------------------------------------# # printHashByValue.pl # #----------------------------------------------------------------------# #----------------------------------------------------------------------# # 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 "\nGRADES IN ASCENDING NUMERIC ORDER:\n"; foreach $key (sort hashValueAscendingNum (keys(%grades))) { print "\t$grades{$key} \t\t $key\n"; } print "\nGRADES IN DESCENDING NUMERIC ORDER:\n"; foreach $key (sort hashValueDescendingNum (keys(%grades))) { print "\t$grades{$key} \t\t $key\n"; }
Although this "Perl hash sort by value" 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
Related Perl hash tutorials
I hope you found this Perl hash tutorial helpful. We have many more hash tutorials on this site, including the following:
Getting started Perl hash tutorials:
- Perl hash introduction/tutorial
- Perl foreach and while: how to loop over the elements in a Perl hash
- Perl hash add - How to add an element to a Perl hash
- How to print each element in a Perl hash
More advanced hash tutorials:
Sorting a hash by key or value:
- Perl hash sorting - Sort a Perl hash by the hash key
- Perl hash sorting - Sort a Perl hash by the hash value