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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?