Created
December 21, 2012 19:37
-
-
Save fprochazka/4355211 to your computer and use it in GitHub Desktop.
Sorting finder results - directory first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| Finder::extensionMethod('sort', function (Finder $finder, $callback) { | |
| $result = iterator_to_array($finder); | |
| uasort($result, $callback); | |
| return new \ArrayIterator($result); | |
| }); | |
| $index = Finder::find('*')->in($dir)->sort(function (\SplFileInfo $a, \SplFileInfo $b) { | |
| return $a->isDir() ? ($b->isDir() ? strnatcasecmp($a->getBasename(), $b->getBasename()) : -1) | |
| : ($b->isDir() ? 1 : ( | |
| strcasecmp($a->getExtension(), $b->getExtension()) == 0 | |
| ? strnatcasecmp($a->getBasename(), $b->getBasename()) : strcasecmp($a->getExtension(), $b->getExtension()) | |
| )); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment