Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts

    Sorting Iterators?

    I've had a look around, but can't seem to figure out the best way to, for example, sort the files in a DirectoryIterator. I have code that looks like:
    PHP Code:
    $dirname '/some/path/to/a/directory';
    $files = new DirectoryIterator($dirname);
    foreach(
    $files as $file) {
      echo 
    $file->getFileName()."\n";

    but I'd quite like to sort $files before looping, ideally by providing a callback, as with usort() on arrays. With a collection that I'm creating, the sorting is trivial, but the underlying collection in DirectoryIterator doesn't seem to be accessible- or I haven't been able to find it if it is.
    DirectoryIterator and SPLFileObject provide a really nice way to do things with files so it seems a shame to re-implement the functionality just to extend it. Anyone managed to do what I want?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    you could use scandir which takes an order flag as it's second argument

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    SPL has filterIterators which can be used to filter the results but I don't think you will currently be able to sort them (without storing/sorting/iterating again) ... documentation is still sketchy
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •