案例排序由A-Z提升,0-9下降?

时间:2021-04-01 03:51:27

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

我想用basename来排序,但也要用一个特定的方法。

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?

我总是希望前面的5位数字按数字顺序排好,然后让alpha排序提升(za),然后将最后的数字排序下降。我该如何做到呢?

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 个解决方案

#1


0  

  1. Extract filename from path: 10396-main-599
  2. 从路径中提取文件名:10396-main-599。
  3. Split filename with '-' as a separator 10396,main,599
  4. 分割文件名与'-'作为分隔符10396,main,599。
  5. 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"]

$split_path1和$split_path2分别为["10396"、"main"、"599"]和["10396"、"alt"、"1"]

The result will be:

结果将是:

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

#1


0  

  1. Extract filename from path: 10396-main-599
  2. 从路径中提取文件名:10396-main-599。
  3. Split filename with '-' as a separator 10396,main,599
  4. 分割文件名与'-'作为分隔符10396,main,599。
  5. 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"]

$split_path1和$split_path2分别为["10396"、"main"、"599"]和["10396"、"alt"、"1"]

The result will be:

结果将是:

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