免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9339 | 回复: 3
打印 上一主题 下一主题

perl 使用sort怎么实现多字段的数字排序 [复制链接]

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
1 [报告]
发表于 2013-01-09 14:00 |显示全部楼层
  1. map{print $_->[0]}sort{$a->[1] <=> $b->[1]}map{[$_,(split(/(\d+)/))[1]]}<DATA>;
  2. __DATA__
  3. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 283)
  4. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 29
  5. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 367)
  6. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 419)
  7. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 429)
  8. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 436)
  9. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 441)
  10. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 459)
  11. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 481)
  12. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 485)
  13. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 486)
  14. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 170)
  15. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 181)
  16. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 21
  17. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 223)
  18. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 250)
  19. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 282)
  20. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 283)
  21. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 286)
  22. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 296)
  23. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 29
  24. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 309)
  25. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 335)
  26. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 337)
  27. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 367)
  28. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 371)
  29. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 376)
  30. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 395)
  31. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 40
复制代码

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
2 [报告]
发表于 2013-01-09 15:15 |显示全部楼层
本帖最后由 mcshell 于 2013-01-09 15:16 编辑

回复 4# jiang870320


    别。。我也是跟着版主和一些低调的人偷着学习的。
split(/(\d+)/)
会以数字分割,注意这里是有括号的。表示匹配的自身也捕获。
然后放在一个匿名数组里,然后用sort进行排序。
这种排序方法是一个施瓦茨转化。。具体看仙子的帖子:
http://bbs.chinaunix.net/thread-3768308-1-1.html

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
3 [报告]
发表于 2013-01-14 10:25 |显示全部楼层
回复 12# dahe_1984


    数组切片

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
4 [报告]
发表于 2013-01-14 22:23 |显示全部楼层
回复 15# inchonline
perldoc -f split
...
  If the PATTERN contains parentheses, additional list elements
               are created from each matching substring in the delimiter.

                   split(/([,-])/, "1-10,20", 3);

               produces the list value

                   (1, ’-’, 10, ’,’, 20)
加括号以后会捕获定义的分隔符。前面貌似说过了{:3_204:}


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP