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

时间:2014-10-27 19:36:13

标签: php sorting

我有像

这样的图像数组
    [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
        )

我想用basename排序,也可以用alphanum按特定方式排序

我总是希望在按字母顺序升序(z-a)之后按数字排序前5位数字,然后将最后的数字按降序排序。我怎么做到这一点?

示例输出应该是这样的

    [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 个答案:

答案 0 :(得分:0)

  1. 从路径中提取文件名:10396-main-599
  2. 使用'分割文件名 - '作为分隔符10396main599
  3. 使用比较功能对路径进行排序

    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];
      }
    }
    
  4. 其中$split_path1$split_path2是[&#34; 10396&#34;,&#34; main&#34;,&#34; 599&#34;]和[&#34; 10396&#34;&#34; ALT&#34;&#34; 1&#34;]

    结果将是:

    ["10396","main","599"]
    ["10396","alt","1"]