Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting PHP Iterators

Tags:

iterator

php

Is there a simple way to sort an iterator in PHP (without just pulling it all into an array and sorting that).

The specific example I have is a DirectoryIterator but it would be nice to have a solution general to any iterator.

$dir = new DirectoryIterator('.');
foreach ($dir as $file)
    echo $file->getFilename();

I'd like to be able to sort these by various criteria (filename, size, etc)

like image 682
Greg Avatar asked Oct 30 '08 18:10

Greg


1 Answers

There is no way to do that. An iterator should "iterate" through the list. You have to sort the underlying list to achieve the needed behavior.

By the way, the more complete reference to the SPL is here: http://www.php.net/~helly/php/ext/spl/

like image 66
andy.gurin Avatar answered Oct 20 '22 14:10

andy.gurin