To sort an array in descending order in Perl, you can use the `sort` function with a custom comparison block.
In this example,
@numbers
.sort
function with a custom comparison block to sort the array of integers in descending order.@numbers = (5, 2, 8, 1, 9);
@sorted_numbers = sort { $b <=> $a } @numbers;
print "Sorted array in descending order: @sorted_numbers\n";
Sorted array in descending order: 9 8 5 2 1
In this example,
@words
.sort
function with a custom comparison block to sort the array of strings in descending order.@words = ("apple", "banana", "cherry", "date");
@sorted_words = sort { $b cmp $a } @words;
print "Sorted array in descending order: @sorted_words\n";
Sorted array in descending order: date cherry banana apple
In this tutorial, we learned How to Sort an Array in Descending Order in Perl language with well detailed examples.