@a = sort map { int(rand(91))+10 } 1..5;
In the absence of a sort comparison block, the default sort ordering is lexicographic-ascending (see Lexicographical order). This ordering produces surprising 'maximum' values — and also 'minimum' values if negative numbers are considered. An explicit numeric-ascending comparison block is needed if $a[0] and $a[$#a] are to make sense as numeric min/max values.
>perl -wMstrict -MData::Dump -le
"my @raw = (1, 2, 3, -1, -2, -12, 123, 23);
;;
my @sorted_lexical_ascending = sort @raw;
dd \@sorted_lexical_ascending;
;;
my @sorted_numeric_ascending = sort { $a <=> $b } @raw;
dd \@sorted_numeric_ascending;
"
[-1, -12, -2, 1, 123, 2, 23, 3]
[-12, -2, -1, 1, 2, 3, 23, 123]
|