文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>列表排序sort

列表排序sort

时间:2008-07-21  来源:bing_fox

背景:     在读代码时发现一个奇怪的东西,$a<=>$b.应用于sort函数,于是有说一下的必要.   正文:   1.基本语法 @sorted = sort {regular} @nosort;   2.一般用法 @sorted = sort(@nosort);    >>>按字符顺序从小到大 @sorted = reverse sort(@nosort); >>>从大到小   3.标准用法
<=>

cmp

$a<=>$b   >>>数字从大到小 $a cmp $b >>>字母从大到小   $b <=> $a >>>数字从小到大 $b cmp $a >>>字母从小到大   @sorted = sort { $a <=> $b } @not_sorted # numerical sort or @sorted = sort { $a cmp $b } @not_sorted # ASCII-betical sort or better @sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort   @sorted = sort { $hash{$a} cmp $hash{$b} } keys %hash; Get a reverse sort of a list @sorted = sort { $b cmp $a } @list; or @sorted = reverse sort { $a cmp $b } @list;   总结: 也许使用标准方法才能看出sort的真正设计意图.
相关阅读 更多 +
排行榜 更多 +
gg

gg

游戏工具 下载
超能格斗大师

超能格斗大师

动作格斗 下载
代驾司机管家

代驾司机管家

交通出行 下载