php sort filenames with an underscore

前端 未结 3 1381
滥情空心
滥情空心 2020-12-20 06:48

I have an array of filenames that I aquire using DirectoryIterator. I am trying to get the filenames to sort so they would be in order like so, this is the way they appear o

相关标签:
3条回答
  • 2020-12-20 07:18

    Not sure what you can do here. Lexically, the underscore has a higher ASCII value than any alphanumeric character.

    Replacing the underscore with a low ASCII value like \x01, then sorting, then replacing the low ASCII value with an underscore, will give the result you want, but that seems pretty expensive for a trivial change in order.

    0 讨论(0)
  • 2020-12-20 07:20

    You can use the php usort method, check it out here with usort you can implement your custom compare to function and sort the array according to it.

    the custom compare to function is int callback ( mixed $a, mixed $b ) , you should return a value less than 0 if $a < $b , zero if equal and a value bigger than 0 when $a > $b

    implement your preferred order of sorting using this method

    example:

    function cmp($a, $b) {
      $aTemp = str_replace('_', '0', $a);
      $bTemp = str_replace('_', '0', $b);     
      return strcmp($aTemp,$bTemp);
    }
    
     usort($arr, "cmp");
    
    0 讨论(0)
  • 2020-12-20 07:24

    try using

    for(i=0;i<array(6).length;i++){
    if (array(6).charAt(0)=='_'){
    for (int i = 0; i < n; i++) 
    {
    for (int j = i + 1; j < n; j++) 
    {
    if (a[i] > a[j]) 
    {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }}}}}
    
    0 讨论(0)
提交回复
热议问题