Perl:Sorting an Array
You can sort an array in Perl with
Sort an array
If you use the
sort function without any custom blocks or function names, it will sort according to ascending string order. (That means numbers will be converted to strings and be sorted as strings.)
Sort the list "one","two","three".
Sort an array using an anonymous custom comparator
You can create your own comparator. This mens that you decide how the sort is to be done.
You must provide a block to the
sort function. The block must evaluate to a negative, zero or a positive
integer depending on how $a compares to $b.
return -1 (or any negative number) if $a comes before $b return 0 if $a and $b are the same return 1 (or any positive number) if $a comes after $b
You may use the built-in operators
cmp, for the ascending numeric and string comparisons, respectively. (i.e.
$a <=> $b will return -1, 0, or 1 based on an ascending numeric order.)