Skip to content

Commit 25c8806

Browse files
committed
feature #46126 [Finder] Case insensitive file sort (hmoreau)
This PR was merged into the 6.2 branch. Discussion ---------- [Finder] Case insensitive file sort | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#16735 Add a second argument **$useCaseInsensitive** to sortByName() method to select between case sensitive and case insensitive sort. Commits ------- 7fdbb61 [Finder] Add case insensitive sort
2 parents 57b24fe + 7fdbb61 commit 25c8806

10 files changed

+175
-1
lines changed

src/Symfony/Component/Finder/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add `Finder::sortByCaseInsensitiveName()` to sort by name with case insensitive sorting methods
8+
49
6.0
510
---
611

src/Symfony/Component/Finder/Finder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,22 @@ public function sortByName(bool $useNaturalSort = false): static
440440
return $this;
441441
}
442442

443+
/**
444+
* Sorts files and directories by name case insensitive.
445+
*
446+
* This can be slow as all the matching files and directories must be retrieved for comparison.
447+
*
448+
* @return $this
449+
*
450+
* @see SortableIterator
451+
*/
452+
public function sortByCaseInsensitiveName(bool $useNaturalSort = false): static
453+
{
454+
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE : Iterator\SortableIterator::SORT_BY_NAME_CASE_INSENSITIVE;
455+
456+
return $this;
457+
}
458+
443459
/**
444460
* Sorts files and directories by type (directories before files), then by name.
445461
*

src/Symfony/Component/Finder/Iterator/SortableIterator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class SortableIterator implements \IteratorAggregate
2727
public const SORT_BY_CHANGED_TIME = 4;
2828
public const SORT_BY_MODIFIED_TIME = 5;
2929
public const SORT_BY_NAME_NATURAL = 6;
30+
public const SORT_BY_NAME_CASE_INSENSITIVE = 7;
31+
public const SORT_BY_NAME_NATURAL_CASE_INSENSITIVE = 8;
3032

3133
/** @var \Traversable<string, \SplFileInfo> */
3234
private \Traversable $iterator;
@@ -51,6 +53,14 @@ public function __construct(\Traversable $iterator, int|callable $sort, bool $re
5153
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
5254
return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
5355
};
56+
} elseif (self::SORT_BY_NAME_CASE_INSENSITIVE === $sort) {
57+
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
58+
return $order * strcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
59+
};
60+
} elseif (self::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE === $sort) {
61+
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
62+
return $order * strnatcasecmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
63+
};
5464
} elseif (self::SORT_BY_TYPE === $sort) {
5565
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
5666
if ($a->isDir() && $b->isFile()) {

0 commit comments

Comments
 (0)