Broad Network

Sorting Perl Hashes

Using Perl Hashes – Part 4

Perl Course

Foreword: In this part of the series I explain how to sort a hash by keys and by values.

By: Chrysanthus Date Published: 3 Nov 2015


This is part 4 of my series, Using Perl Hashes. In this part of the series I explain how to sort a hash by keys and by values. You should have read the previous parts of the series before coming here, as this is the continuation.

Sorting a Hash by Keys
To sort by keys is simple: just use the keys() function to return a list of keys. Pass the list of keys to the sort function. After that use the sorted keys to display the values of the hash. The procedure is:

        my @keys = sort keys %hash;

        print "$hash{$_} " foreach @keys;

Try the following code:

use strict;

    my %fruitColor = (Banana => "yellow", Pear => "green", Apple => "purple", Lemon => "green", Pineapple =>);

    my @keys = sort keys %fruitColor;

    print "$_, $fruitColor{$_}\n" foreach @keys;

The output is:

    Apple, purple
    Banana, yellow
    Lemon, green
    Pear, green

At the end of the day it is the elements that have been sorted, and not just the keys without the values.

Note, you cannot sort a hash and then send the sorted elements back to the hash; because the hash will naturally go back to disorder. After sorting, you use the elements, e.g. by printing them out.

Sorting a Hash by Values
The problem of sorting a hash by value is that there may be duplicate values. This is not difficult to solve: whenever there is more than one value, you sort using the keys. So, you end up sorting by value and then by keys. The procedure is:

    my @keys = sort { $hash{$a} <=> $hash{$b} } keys %hash;

Now, the sort() function is not just what I have been presenting. The sort() function can take two arguments: a block and a list (array). In this case, there is no comma separating the block and the list. In this code, $a and $b are special variables for the sort() function. So you should not touch them. You change only %hash for the name of the hash. I will come back to the sort() function in a different series. Just read and try the following code, which sorts first by values and then by keys:

use strict;

    my %fruitColor = (Banana => "yellow", Pear => "green", Apple => "purple", Lemon => "green", Pineapple =>);

    my @keys = sort { $fruitColor{$a} <=> $fruitColor{$b} } keys %fruitColor;

    print "$_, $fruitColor{$_}\n" foreach @keys;

The output is:

    Lemon, green
    Apple, purple
    Pear, green
    Banana, yellow

This may not be what you wanted. Maybe you wanted the two “green” values to appear next to one another (with their associated keys). I will come back to the explanation of the sort() function in a different series, to solve that problem.

That is it for this part of the series. We stop here and continue in the next part.


Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course



Become the Writer's Fan
Send the Writer a Message