8000 [Finder] added "use natural sort" option by vyshkant · Pull Request #27024 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Finder] added "use natural sort" option #27024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Finder] added "use natural sort" option
  • Loading branch information
vyshkant committed Apr 24, 2018
commit e697c7d2721d30884193ed86c5f6fd6f044323ab
5 changes: 5 additions & 0 deletions src/Symfony/Component/Finder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* added $useNaturalSort option to Finder::sortByName() method

4.0.0
-----

Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,17 @@ public function sort(\Closure $closure)
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @param bool $useNaturalSort Whether to use natural sort or not, disabled by default
*
* @return $this
*
* @see SortableIterator
*/
public function sortByName()
public function sortByName(/* bool $useNaturalSort = false */)
{
$this->sort = Iterator\SortableIterator::SORT_BY_NAME;
$useNaturalSort = 0 < func_num_args() && func_get_arg(0);

$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL : Iterator\SortableIterator::SORT_BY_NAME;

return $this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Finder/Iterator/SortableIterator.php
7657
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SortableIterator implements \IteratorAggregate
const SORT_BY_ACCESSED_TIME = 3;
const SORT_BY_CHANGED_TIME = 4;
const SORT_BY_MODIFIED_TIME = 5;
const SORT_BY_NAME_NATURAL = 6;

private $iterator;
private $sort;
Expand All @@ -41,6 +42,10 @@ public function __construct(\Traversable $iterator, $sort)
$this->sort = function ($a, $b) {
return strcmp($a->getRealpath() ?: $a->getPathname(), $b->getRealpath() ?: $b->getPathname());
};
} elseif (self::SORT_BY_NAME_NATURAL === $sort) {
$this->sort = function ($a, $b) {
return strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
};
} elseif (self::SORT_BY_TYPE === $sort) {
$this->sort = function ($a, $b) {
if ($a->isDir() && $b->isFile()) {
Expand Down
Loading
0