Perl | Sorting of Arrays - GeeksforGeeks

07 May, 2019
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:
Use of ASCII values to sort an ArrayUse of Comparison function (cmp)Alphabetical order of Sorting(Case insensitive)Sorting of an Array of Numbers
Use of ASCII values to sort an Array
Use of Comparison function (cmp)
Alphabetical order of Sorting(Case insensitive)
Sorting of an Array of Numbers
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 arrayprint sort @country;
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.
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 arrayprint "Array in ascending order: @x\n";  # Printing sorted arrayprint "Array in descending order: @y";
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 fruitsStep 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’.
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 arrayprint "Array after converting to lower case: @x\n";  # Printing sorted arrayprint "Array after converting to upper case: @y\n";
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
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 Arrayprint "Original Array: @n\n";  # Sorting numbers with use of# spaceship operator@x = sort { $a <=> $b } @n;  # Printing sorted arrayprint "Array after Sorting: @x";
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.
Perl-Arrays
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Perl | Polymorphism in OOPs
Perl | Regular Expressions
Perl Tutorial - Learn Perl With Examples
Perl | Subroutines or Functions
Perl | Boolean Values
Introduction to Perl
Perl | length() Function
Perl | substitution Operator
Perl | sleep() Function
Perl | Regex Cheat Sheet