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 Array
- Use of Comparison function (cmp)
- Alphabetical order of Sorting(Case insensitive)
- 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:
Perl
#!/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:
Perl
#!/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:
Perl
#!/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.
Perl
#!/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.
Similar Reads
Sorting Hash in Perl
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 examp
6 min read
Sorting of Arrays in Julia
The process of arranging the given data in a particular order or manner depends on the output. It is storing of data in sorted order. Sorting can be done in two ways i.e in ascending and descending order. Sorting can be performed using sorting algorithm or sorting functions. So, in sorting the progr
8 min read
JavaScript - Sort an Array of Strings
Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana',
3 min read
Perl | sort() Function
sort() function in Perl is used to sort a list with or without the use of method of sorting. This method can be specified by the user in the form of subroutines or blocks. If a subroutine or block is not specified then it will follow the default method of sorting. Syntax: sort List sort block, List
2 min read
Sorting mixed Strings in Perl
Sorting in Perl can be done with the use of a pre-defined function 'sort'. This function uses a quicksort algorithm to sort the array passed to it. Syntax: sort @array Returns: a sorted array Sorting of an array that contains strings in the mixed form i.e. alphanumeric strings can be done in various
4 min read
How to Sort an Array in Scala?
Sorting arrays effectively is essential for many applications, regardless of whether you're working with texts, custom objects, or numerical data. Because Scala is a strong and expressive language, it provides a variety of array sorting methods that may be customized to fit various needs and situati
5 min read
How to Sort an Arrays of Object using Arrays.sort()
To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort(). Sorting an Array of ObjectWith Array of Object what it really means is an array st
3 min read
How to Sort an Array in TypeScript ?
Array sorting is the process of arranging the elements within an array in a specified order, often either ascending or descending based on predetermined criteria. Below are the approaches used to sort an array in typescript: Table of Content Method 1: Using sort methodMethod 2: Spread OperatorMethod
3 min read
Perl Reverse Sort Method
The article focuses on discussing how to sort an array/ list which consists of integers or strings in reverse order (descending to ascending). Prerequisite: Sorting in Perl. Syntax: reverse sort @array_name; reverse sort (@array_name); This method is a combination of the reverse and sort method of P
3 min read
Sort An Array Of Arrays In JavaScript
The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 min read