Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mablae/f4c67f5b5bdf1b8a7c41524148a16b7a to your computer and use it in GitHub Desktop.
Save mablae/f4c67f5b5bdf1b8a7c41524148a16b7a to your computer and use it in GitHub Desktop.
Sortable DirectoryIterator based on last modification time
class SortableDirectoryIterator extends RecursiveDirectoryIterator
{
/**
* \ArrayObject
*/
private $dirArray;
public function __construct(string $path)
{
parent::__construct($path);
$this->dirArray = new \ArrayObject();
foreach($this as $item) {
$this->dirArray->append( $item );
}
$this->dirArray->uasort( function ($fileObj1, $fileObj2) {
if ($fileObj1->getMTime() == $fileObj2->getMTime()) {
return 0;
}
return ($fileObj1->getMTime() < $fileObj2->getMTime()) ? -1 : 1;
} );
}
public function getIterator()
{
return $this->dirArray->getIterator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment