Skip to content

Instantly share code, notes, and snippets.

@tomaskavalek
Created February 17, 2023 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaskavalek/21086ec47c453d220ba5f0b69dce19c9 to your computer and use it in GitHub Desktop.
Save tomaskavalek/21086ec47c453d220ba5f0b69dce19c9 to your computer and use it in GitHub Desktop.
Sortable solution for DirectoryIterator
<?php
class SortableDirectoryIterator implements IteratorAggregate
{
private $storage;
public function __construct($path)
{
$this->storage = new ArrayObject();
$files = new DirectoryIterator($path);
foreach ($files as $file) {
$this->storage->offsetSet($file->getFilename(), $file->getFileInfo());
}
$this->storage->uksort(
function ($a, $b) {
return strcmp($a, $b);
}
);
}
public function getIterator()
{
return $this->storage->getIterator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment