- 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.
@n = (12, 44, 2, 5, 25, 7, 96, 1);
print "Original Array: @n\n";
@x = sort { $a <=> $b } @n;
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
No comments