![]() |
VOOZH | about |
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 Perl. The reverse method is used to simply reverse a scalar or an array and the sort method is used to sort an array. The reverse sort function combines these two functionalities to sort an array in reverse order.
Below is the Perl program to sort a list of numbers normally.
8 5 3 2 1
Below is the Perl program to sort a list of numbers using block.
8 5 3 2 1
Below is the Perl program to sort a list of numbers using subroutine.
8 5 3 2 1
works to this sort reverse is how
Explanation:
Here all the string starts with lowercase letters, no mixture has been considered for this purpose. We will use mixtures in later stages.
Below is the Perl program to sort a list of strings with mixed case.
to sort is Works This Reverse How
Explanation:
Here Perl internally groups those strings which start with Uppercase and sorts them in descending order and the same for the words that start with lowercase letters. Now while printing the entire thing it first prints the reversely sorted lowercase letters then those which start with uppercase letters as the ASCII value of 'a' is 97 and 'A' is 65, as we are sorting reversely that's why the higher ASCII value is being considered as a whole.
If we have some strings which are of mixed string, means that if the starting one is lowercase but anyone in the middle or end is uppercase or vice versa the result will be the same as last time only the case of the first letter matters. Below is the Perl program to sort a list of strings if the strings are of the mixed case.
tO sort iS Works This Reverse How