Perl has two operators <=> and cmp,
which are very useful when wishing to sort arrays. $a <=> $b returns -1 if $a is numerically lesser than $b, 1 if it's greater, and zero if they are equal.
cmp does the same for string comparison. For instance the previous example could be re-written as:
Much more civil, isn't it? The following example, sorts an array of strings in reverse:
转载于:https://blog.51cto.com/80888888/1333282