Prerequisite: Perl | Hashes Set of key/value pair is called a Hash. Each key in a hash structure are unique and of type strings. The values associated with these keys are scalar. These values can either be a number, string or a reference. A Hash is declared using my keyword. Let us consider an example to understand the concept of sorting the hash.
Example: Consider a hash with the names of the students in a class and with their average score. Here, the name of the students are the keys and their average scores are the values. Print this hash using foreach loop on the student names returned by the keys function.
Perl
use strict;
use warnings;
use 5.010;
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
foreach my $name ( keys %studentnames ) {
printf "%-8s %s \n", $name , $studentnames { $name };
}
|
Output:
Marty 16.5
Jason 25.2
Plato 39
Vivek 27
Socrates 29.5
Martha 14
Earl 31
Uri 19.6
Nitin 30
Time Complexity: O(n)
Auxiliary Space: O(n)
Note: The output may contain any random order depends on the system and the version of Perl. Hashes can be sorted in many ways as follows:
- Sorting the Hash according to the ASCII values of its keys: Generally, sorting is based on ASCII table. It means sorting will keep all the upper-case letters in front of all the lower-case letters. This is the default behavior of the sorting. The above example code is sorted according to the ASCII values of its keys.
- Sorting the Hash according to the alphabetical order of its keys: Here, the keys are sorted alphabetically.
Example:
Perl
use strict;
use warnings;
use 5.010;
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
foreach my $name ( sort { lc $a cmp lc $b } keys %studentnames )
{
printf "%-8s %s \n", $name , $studentnames { $name };
}
|
Output:
Earl 31
Jason 25.2
Martha 14
Marty 16.5
Nitin 30
Plato 39
Socrates 29.5
Uri 19.6
Vivek 27
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Sorting according to the values of Hash: You can also sort the hash according to the values of hash as follows:
Example 1: Sorting the hash values according to the ASCII table.
Perl
use strict;
use warnings;
use 5.010;
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
foreach my $name ( sort values %studentnames )
{
say $name ;
}
|
14
16.5
19.6
25.2
27
29.5
30
31
39
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Example 2: Sorting according to the numerical value of Values of hash as follows:
Perl
use strict;
use warnings;
use 5.010;
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
foreach my $name ( sort { $a <=> $b } values %studentnames )
{
say $name ;
}
|
14
16.5
19.6
25.2
27
29.5
30
31
39
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Sort the keys of the hash according to the values: You can also sort the keys of hash according to the given values. Example 1: In the below program, <=> is termed as the spaceship operator. If you will run the code again and again then you can notice the difference in the output. Sometimes you find Plato before the Nitin and vice-versa.
Perl
use strict;
use warnings;
use 5.010;
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 45,
Plato => 45,
);
foreach my $name ( sort { $studentnames { $a } <=>
$studentnames { $b }} keys %studentnames )
{
printf "%-8s %s \n", $name , $studentnames { $name };
}
|
Martha 14
Marty 16.5
Uri 19.6
Jason 25.2
Vivek 27
Socrates 29.5
Earl 31
Plato 45
Nitin 45
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Example 2: To solve the above code, keys having the same values can be sorted according to the ASCII table as follows:
Perl
use strict;
use warnings;
use 5.010;
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 45,
Plato => 45,
);
foreach my $name ( sort { $studentnames { $a } <=> $studentnames { $b } or
$a cmp $b } keys %studentnames ) {
printf "%-8s %s \n", $name , $studentnames { $name };
}
|
Output:
Martha 14
Marty 16.5
Uri 19.6
Jason 25.2
Vivek 27
Socrates 29.5
Earl 31
Nitin 45
Plato 45
Explanation: Here you can see the key Nitin and Plato are sorted according to the ASCII table. It doesn’t matter how many times you run the code, the output will remain the same.
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Apr, 2023
Like Article
Save Article