dongtui4038 2014-10-27 19:36
浏览 104
已采纳

案例按A-Z升序和0-9降序排序?

I have image arrays such as

    [Images] => Array
        (
            [0] => /var/www/httpdocs/images/products/detail/10396-alt-1.jpg
            [1] => /var/www/httpdocs/images/products/detail/10396-main-599.jpg
        )

    [Images] => Array
        (
            [0] => /var/www/httpdocs/images/products/category/10167-main-354.jpg
            [1] => /var/www/httpdocs/images/products/detail/10167-alt-1.jpg
            [2] => /var/www/httpdocs/images/products/detail/10167-alt-2.jpg
            [3] => /var/www/httpdocs/images/products/category/10168-main-361.jpg
            [4] => /var/www/httpdocs/images/products/category/10168-main-360.jpg
        )

That I would like to sort by basename, but also by alphanum in a specific way

I would always like the first 5 digits to be sorted by numerically after having the alpha sorted ascending (z-a) then also have the last digits sorted descending. How would I accomplish this?

Sample output should be like this

    [Images] => Array
        (
            [0] => /var/www/httpdocs/images/products/detail/10396-main-599.jpg
            [1] => /var/www/httpdocs/images/products/detail/10396-alt-1.jpg
        )

    [Images] => Array
        (
            [0] => /var/www/httpdocs/images/products/category/10167-main-354.jpg
            [1] => /var/www/httpdocs/images/products/category/10168-main-360.jpg
            [2] => /var/www/httpdocs/images/products/category/10168-main-361.jpg
            [3] => /var/www/httpdocs/images/products/detail/10167-alt-1.jpg
            [4] => /var/www/httpdocs/images/products/detail/10167-alt-2.jpg
        )
  • 写回答

1条回答 默认 最新

  • doudou20080720 2014-10-27 19:47
    关注
    1. Extract filename from path: 10396-main-599
    2. Split filename with '-' as a separator 10396,main,599
    3. Sort path using comparison function

      function is_path_less($split_path1,$split_path2){
        if($split_path1[0] == $split_path2[0]){
          if($split_path1[1] == $split_path2[1]){
            return $split_path1[2] < $split_path2[2];
          }else{
            return $split_path1[1] > $split_path2[1];
          }
        }else{
          return $split_path1[0] < $split_path2[0];
        }
      }
      

    Where $split_path1 and $split_path2 are ["10396","main","599"] and ["10396","alt","1"]

    The result will be:

    ["10396","main","599"]
    ["10396","alt","1"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)