phpsymfonysymfony-componentssymfony-finder

How can I sort files by DESC order with Symfony Finder Component?


By default Symfony Finder Component sorts files by ASC order.

//sorting by ASC order
$finder->files()->in($this->getDumpPath())->sortByModifiedTime();

How can I sort files by DESC?


Solution

  • You may use the sort method and give your own sort anonymous function (see Symfony\Component\Finder\Iterator\SortableIterator)

    $finder->sort(function ($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); });
    

    This is all about sorting tips. It's always the same thing with that kind of job. Please take a look to the usort function.

    To be more precise, I've just take a code snipet from Symfony\Component\Finder\Iterator\SortableIterator, and I've reverted the return condition.