Open In App

Perl | Sorting of Arrays

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Perl has a built-in sort() function to sort an array of alphabets and numbers. When an array is passed to the sort() function it returns a sorted array.

Syntax: sort @Array

Returns: a sorted array

Sorting of Arrays in Perl can be done in multiple ways:

  1. Use of ASCII values to sort an Array
  2. Use of Comparison function (cmp)
  3. Alphabetical order of Sorting(Case insensitive)
  4. Sorting of an Array of Numbers
Use of ASCII values to sort an Array

As the ASCII values of the upper case letters are less than that of the lowercase letters, all the values with the uppercase are arranged before the lower case values start.
Example:




#!/usr/bin/perl
  
# Initializing an array
@country = ('India', 'America', 'london'
            'france', 'bangladesh');
  
# Printing sorted array
print sort @country;


Output:

AmericaIndiabangladeshfrancelondon

Here is how the above program works:
Step 1- As the ASCII values of Upper case letter(A starting from 65) is less than lower case letter(a starting from 97), all the uppercase letter words are arranged before the lower case letter words.
Step 2- In the above example, the array has the name of countries wherein few words start with uppercase while the other start with the lower case so all the values with uppercase are sorted before sorting those with lower case values.

Use of Comparison function (cmp)

In this type of sorting technique, the iterator goes over every two elements of the original array. With each iteration, it puts the 1st value into the variable $a, and the 2nd value in the variable $b. Now, the cmp function is called and these values are passed in the form of a block. Now this function will return -1 if the content of $a should be kept on the left whereas it returns 1 if the content of $b should be on the left.
This function can be used to sort the Array in descending order. sort() function by default uses the cmp() but without the use of block.
Example:




#!/usr/bin/perl
  
# Initializing an array
@fruits = ('chikoo', 'apple', 'banana',
           'orange', 'grapes');
  
# Sorting array in ascending order
@x = sort { $a cmp $b } @fruits;
  
# Sorting array in descending order
@y = sort { $b cmp $a } @fruits;
  
# Printing sorted array
print "Array in ascending order: @x\n";
  
# Printing sorted array
print "Array in descending order: @y";


Output:

Array in ascending order: apple banana chikoo grapes orange
Array in descending order: orange grapes chikoo banana apple

Here is how the above program works:
Step 1- An array is initialized with the name of fruits
Step 2- It fetches the values from left in $a and from right in $b and compares these two values using cmp function. So, in the example above ‘Grapes’ is compared with ‘chikoo’.

Alphabetical order of Sorting(Case insensitive)

In order to sort an array that contains values which are both in uppercase as well as lowercase, these values are required to be converted to either uppercase or lowercase for comparison purpose. Further, cmp() function will be used to sort the array.

Note: The original values of the array will not be modified.
Example:




#!/usr/bin/perl
  
# Initializing an array
@fruits = ('Chikoo', 'Apple', 'banana',
           'orange', 'Grapes');
  
# Converting values to lower case for
# comparison before sorting
@x = sort { lc($a) cmp lc($b) } @fruits;
  
# Converting values to upper case for
# comparison before sorting
@y = sort { uc($a) cmp uc($b) } @fruits;
  
# Printing sorted array
print "Array after converting to lower case: @x\n";
  
# Printing sorted array
print "Array after converting to upper case: @y\n";


Output:

Array after converting to lower case: Apple banana Chikoo Grapes orange
Array after converting to upper case: Apple banana Chikoo Grapes orange

Here is how the above program works :
Step 1- Initializing an array with values.
Step 2- Converting the values to lowercase and uppercase one by one irrespective of their original case followed by comparison using cmp function and sorting it in ascending order.

Note: It can be seen that there’s no difference in the output in both the cases

Sorting of an Array of Numbers

If an array containing number is sorted using sort function it takes every value in the array as a string so 12 will be placed before 2. Therefore, to consider the values as a number, spaceship operator() is used instead of cmp in the sort function. This operator considers its operands as a number and sorts the data as a number.




#!/usr/bin/perl
  
# Initializing an array
@n = (12, 44, 2, 5, 25, 7, 96, 1);
  
# Printing Original Array
print "Original Array: @n\n";
  
# Sorting numbers with use of
# spaceship operator
@x = sort { $a <=> $b } @n;
  
# Printing sorted array
print "Array after Sorting: @x";


Output:

Original Array: 12 44 2 5 25 7 96 1
Array after Sorting: 1 2 5 7 12 25 44 96

Here is how the above program works :
Step 1- Initializing an array with numerical values.
Step 2- Using spaceship operator to consider the values as numbers and returns 1, 0, -1 depending on the values in $a and $b.



Last Updated : 07 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads