doudou3716 2014-07-26 12:58 采纳率: 100%
浏览 24
已采纳

排序字符串数组取决于之后的最后一次出现。?

I have an array containing a list of filenames that I want to sort by extension. Folders first then files sorted by extension :

Consider the code below:

    $arr = array("noext","world.b","hello.a","test.z","lol.f");

Is there anyway how can I sort the array above depending on the extension to get something like the following :

    noext
    hello.a
    world.b
    lol.f
    test.z

I know I could use something like usort with a comparing function. But I have no idea how can I do it.

Any help would be highly appreciated.

  • 写回答

1条回答 默认 最新

  • douyuan6490 2014-07-26 13:09
    关注

    You can indeed use usort:

    <?php
    $arr = array("noext","world.b","hello.a","test.z","lol.f");
    usort($arr, function($s1, $s2) {
      $ext1 = pathinfo($s1, PATHINFO_EXTENSION);
      $ext2 = pathinfo($s2, PATHINFO_EXTENSION);
      return strcmp($ext1, $ext2);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭