Perl Reverse Sort Method
Last Updated :
24 Feb, 2023
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.
1. Sorting a List of Numbers Normally
Below is the Perl program to sort a list of numbers normally.
Perl
#!/usr/bin/perl
@nums = (5,8,1,2,3);
@rev_num = reverse sort ( @nums );
print ( "@rev_num" );
|
2. Sorting a List of Numbers Using Block
Below is the Perl program to sort a list of numbers using block.
Perl
#!/usr/bin/perl
@nums = (5,8,1,2,3);
@rev_num = reverse sort { $a <=> $b } ( @nums );
print ( "@rev_num" );
|
3. Sorting a List of Numbers Using Subroutine
Below is the Perl program to sort a list of numbers using subroutine.
Perl
#!/usr/bin/perl
@nums = (5,8,1,2,3);
@rev_num = reverse sort rev_sort ( @nums );
sub rev_sort{
if ( $a < $b ){
return -1;
}
elsif ( $a == $b ){
return 0;
}
else {
return 1;
}
}
print ( "@rev_num" );
|
4. Sorting a List of Strings (All Starts With Same Case)
Perl
#!/usr/bin/perl
@str = ( "this" , "is" , "to" , "how" , "reverse" , "sort" , "works" );
@rev_str = reverse sort @str ;
print ( "@rev_str" );
|
Output
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.
5. Sorting a List of Strings (Mixed Case)
Below is the Perl program to sort a list of strings with mixed case.
Perl
#!/usr/bin/perl
@str = ( "This" , "is" , "to" , "How" , "Reverse" , "sort" , "Works" );
@rev_str = reverse sort @str ;
print ( "@rev_str" );
|
Output
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.
6. If Strings are of Mixed Case
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.
Perl
#!/usr/bin/perl
@str = ( "This" , "iS" , "tO" , "How" , "Reverse" , "sort" , "Works" );
@rev_str = reverse sort @str ;
print ( "@rev_str" );
|
Output
tO sort iS Works This Reverse How
Share your thoughts in the comments
Please Login to comment...