Perl 编程/关键字/cmp
外观
cmp是一个二元函数,它返回-1, 0,或者1取决于左边的参数在字符串上是否小于、等于或大于右边的参数。换句话说,cmp在文本上做与二元函数<=>相同的事情。
EXPRESSION cmp EXPRESSION
代码
use strict;
use warnings;
my @array = ("Hotel", "Alpha", "Foxtrott", "Bravo", "India", "Charlie", "10", "-10", "9", "-9", "Echo", "Delta");
my @sorted_array = (sort { $a cmp $b } @array);
print join(",", @sorted_array), "\n";
返回按字母顺序排序的数组内容
-10, 10, Alpha, Bravo, Charlie, Delta, Echo, Foxtrott, Hotel, India