Perl

Perl Sort() Function

The sort() is one of the useful functions of Perl to sort the array, list, and hash data in Perl. It returns the sorted value into another variable by keeping the main variable unchanged. This function sorts the data in ascending order by default. However, the data can be sorted in Perl by this function in multiple ways.

Syntax

The syntax of the sort function in Perl is shown in the following. Three types of data can be sorted using the sort() function. These are list, array, and hash:

sort List or Array or Hash

Different Perl Examples of the Sort() Function

The uses of the sort() function to sort the different types of arrays are shown in this part of the tutorial.

Example 1: Sort an Array of String Values

Create a Perl file with the following script that sorts an array of string values. An array of four strings is declared in the script. Here, the Perl “Data::Dumper” module is used to print the array values before and after sorting.

#!/usr/bin/perl
use strict;
use warnings;
use 5.34.0;
use Data::Dumper;

#Declare an array of string values
my @arr_str = ("Perl", "Bash", "Java", "Python");
#Sort the array
my @sorted_array =  sort(@arr_str);

say "The content of the main array:";
#Print the main array using the dumper variable
print Dumper(\@arr_str);
say "The content of the sorted Array:";
#Print the sorted array using the dumper variable
print Dumper(\@sorted_array);

Output:

The following output appears after executing the script. The array values are sorted based on the first alphabetic character of the string. The array values after sorting are “Bash”, “Java”, “Perl”, and “Python”:

Example 2: Sort an Array of Numbers

 

Create a Perl file with the following script that sorts an array of numbers. An array of five numbers is declared in the script. The sorted values of the array are stored in another array variable. Both unsorted and sorted arrays are printed using the “say” function.

#!/usr/bin/perl

use warnings;
use strict;
use 5.34.0;

#Declare an array of numbers
my @arr_num = (567, 23, 896, 34, 671);
#Sort the array values
my @sorted_array = sort(@arr_num);

#Print the main array values
say "The content of the main array: @arr_num";
#Print the sorted array values
say "The content of the sorted Array: @sorted_array";

Output:

The following output appears after executing the script. The sorted values of the array are 23, 34, 567, 671, and 896:

Example 3: Sort the Array of Hashes Based on the Key

Create a Perl file with the following script that sorts the array of hashes based on the key values of the hashes. An array of three hashes is declared in the script. Here, the “id” key contains a numeric value and the “name” key contains a string value in each hash element of the array. Normally, the hash key-value pairs are printed randomly. In this example, the sort() function is used for two purposes. The sort() function is used in the “for” loop to sort the keys of the hashes to print the array of hashes before and after sorting. An array of hashes can be sorted based on one or more keys of the hash element. Here, the sort() function is used to sort the array based on the “id” key of each hash element.

#!/usr/bin/perl
use warnings;
use strict;
use 5.34.0;

#Declare an array of hash
my @students = ({id => '9845', name => 'zafar Iqbal'},
                {id => '2387', name => 'Ahsan Habib'},
                {id => '5698', name => 'Humayan Ahmed'});

#Print the array before sort
say "The content of the main array:";
say "ID\t Name";
for (my $i = 0; $i <= $#students; $i++)
{
    for my $v ( sort keys %{ $students[$i] } )
    {
        print "$students[$i]{$v}\t";
    }
    print "\n";
}

#Sort the array
my @sorted_array = sort {$a->{id} <=> $b->{id}} @students;

#Print the array after sort
say "\nThe content of the sorted Array:";
say "ID\t Name";
for (my $i = 0; $i <= $#sorted_array; $i++)
{
    for my $v ( sort keys %{ $sorted_array[$i] } )
    {
        print "$sorted_array[$i]{$v}\t ";
    }
    print "\n";
}

Output:

The following output appears after executing the script. In the output, the values of the array of hashes are printed in the formatted way where the “id” values are “9845”, “2387”, and “5698”. After sorting, the values of the array of hashes are printed again where the sorted “id” values are “2387”, “5698”, and “9845”:

Conclusion

The sort() function is a very useful function of any programming language. Three different uses of the sort() function in Perl are shown in this tutorial. But this function can also be used for many other purposes. The Perl user will be able to know the basic uses of this function after practicing the examples of this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.