Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP sort filename array by file extension

Options
  • 16-08-2006 9:32pm
    #1
    Closed Accounts Posts: 19,080 ✭✭✭✭


    Right, I have an array called $files. It contains a list of filenames on the format of dummy.jpg and bla.txt etc

    I currently order this abc via filename. I'd like to order it abc via file extension.

    The way I see it I need to order abc by the characters under the last "." in the filename.

    Any advice appreciated?


Comments

  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    If you know they're all going to be 3 letter exensions, you can use substr_compare(), otherwise you can tokenise them using "." and take the final token (strtok is what you need here) and then sort by token using usort() or else you can use strripos to find the last dot in the string, get the substring using the values you take from that, and again sort the resulting array of substrings you create.

    Does that make sense?


  • Registered Users Posts: 1,023 ✭✭✭[CrimsonGhost]


    Use usort and then write your own function which will evaluate 2 filenames and tell you which should come first based on extension.
    http://www.php.net/usort
    It's a fairly easy exercise once you know about usort.


  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    I'll have a look and see Ghost ..


  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    I'm actually trying to sort by (1) extension and (2) abc. Will usort() let me do this too?

    Any suggestions on the code welcomed as I'm not getting my head around this too well :(


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Well, that should be handy enough to do. Basically need to perform both comparisons at the same time. Are you familiar with the bubble sort algorithm? It's a very simple sorting algorithm. If you can grab a copy of that, take a look through, you should understand it easily enough. If not, just print out the entire array after each iteration and you'll see what it's doing.

    So in your bubble sort algorithm in the comparison section what you need to do is first compare the extensions to see which comes first (use substring or tokenising [tokenising is just splitting a word/sentance into parts]) and IF the extension is equal, compare the main filename to see which comes first.

    I assume PHP has some kind of string compare function which returns 0 if strings are equal and -1 or 1 if ones "greater" than or "Less than" the other alphabetically.


  • Advertisement
  • Registered Users Posts: 1,023 ✭✭✭[CrimsonGhost]


    First Mutant is over complicating the issue.
    Use usort on the array, calling your own function foo. Foo will take 2 arguments of the complete file names, split them, compare extensions first. If exts are the same it will compare the names instead.

    Pseudo code for foo
    foo(arg1, arg1)
    {
    split arg1 are arg2 into name1 ext1 and name2 ext2
    if ext1 < ext 2
    return -1
    if ext1 > ext 2
    return 1
    if ext1 == ext2
    Similar comparisons but on name1 and name2
    }

    You'll need to check the docs for hte return values needed to be returned so usort knows which is considered higher or lower in value.


  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    Bah, bloody annoying.


Advertisement