Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, a hash is an unordered collection of key-value pairs. Since hashes are inherently unordered, when we talk about sorting a hash, we're typically referring to sorting the keys of the hash and then retrieving the associated values in that order.
Here's a tutorial on sorting hashes in Perl based on various criteria.
By default, the sort
function will sort the keys in lexicographical (ASCII-betical) order:
#!/usr/bin/perl use strict; use warnings; my %hash = ( 'apple' => 150, 'banana' => 100, 'cherry' => 50, ); foreach my $key (sort keys %hash) { print "$key: $hash{$key}\n"; }
This would output:
apple: 150 banana: 100 cherry: 50
If you want to sort the hash based on its values, you can do the following:
foreach my $key (sort { $hash{$a} <=> $hash{$b} } keys %hash) { print "$key: $hash{$key}\n"; }
This would output:
cherry: 50 banana: 100 apple: 150
To sort by the length of the keys:
foreach my $key (sort { length($a) <=> length($b) || $a cmp $b } keys %hash) { print "$key: $hash{$key}\n"; }
Output:
apple: 150 cherry: 50 banana: 100
If you want to sort in descending order:
# By value foreach my $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) { print "$key: $hash{$key}\n"; } # By key foreach my $key (sort { $b cmp $a } keys %hash) { print "$key: $hash{$key}\n"; }
$a
and $b
are special package global variables that are set to the keys being compared. They are used only for the sort block or function.<=>
operator is the numerical comparison operator, and cmp
is the string comparison operator in Perl.Sorting a hash in Perl is typically done by sorting its keys based on different criteria. With the help of anonymous subroutines, the sort
function provides a flexible way to define custom sorting behavior.
Perl sort hash by keys example:
my %fruit_prices = ('apple' => 2, 'orange' => 1, 'banana' => 3); my %sorted_fruits = map { $_ => $fruit_prices{$_} } sort keys %fruit_prices;
Sorting a hash by values in Perl:
my %fruit_prices = ('apple' => 2, 'orange' => 1, 'banana' => 3); my %sorted_fruits = map { $_ => $fruit_prices{$_} } sort { $fruit_prices{$a} <=> $fruit_prices{$b} } keys %fruit_prices;
How to sort a hash of hashes in Perl:
my %employee_data = ( 'Alice' => { 'age' => 30, 'department' => 'HR' }, 'Bob' => { 'age' => 25, 'department' => 'IT' }, 'Charlie' => { 'age' => 35, 'department' => 'Finance' } ); my %sorted_employees = map { $_ => $employee_data{$_} } sort { $employee_data{$a}->{'age'} <=> $employee_data{$b}->{'age'} } keys %employee_data;
Custom sorting of a hash in Perl:
my %words = ('apple' => 'red', 'banana' => 'yellow', 'grape' => 'purple'); my %sorted_words = map { $_ => $words{$_} } sort { length($words{$a}) <=> length($words{$b}) } keys %words;
Sorting a hash numerically in Perl:
my %numeric_data = ('one' => 1, 'three' => 3, 'two' => 2); my %sorted_numeric = map { $_ => $numeric_data{$_} } sort { $numeric_data{$a} <=> $numeric_data{$b} } keys %numeric_data;
Case-insensitive hash sorting in Perl:
my %case_sensitive = ('apple' => 'red', 'Banana' => 'yellow', 'Grape' => 'purple'); my %case_insensitive = map { $_ => $case_sensitive{$_} } sort { lc($a) cmp lc($b) } keys %case_sensitive;
Perl sort hash by multiple keys:
my %multi_key_data = ('apple' => { 'color' => 'red', 'price' => 2 }, 'banana' => { 'color' => 'yellow', 'price' => 1 }); my %sorted_multi_key = map { $_ => $multi_key_data{$_} } sort { $multi_key_data{$a}->{'color'} cmp $multi_key_data{$b}->{'color'} || $multi_key_data{$a}->{'price'} <=> $multi_key_data{$b}->{'price'} } keys %multi_key_data;
Descending order hash sorting in Perl:
my %descending_data = ('apple' => 2, 'banana' => 1, 'orange' => 3); my %sorted_descending = map { $_ => $descending_data{$_} } sort { $descending_data{$b} <=> $descending_data{$a} } keys %descending_data;
In-place hash sorting in Perl:
my %original_data = ('apple' => 2, 'banana' => 1, 'orange' => 3); my @sorted_keys = sort { $original_data{$a} <=> $original_data{$b} } keys %original_data; @original_data{@sorted_keys} = @original_data{keys %original_data};