Skip to content

[Finder] Add methods to sort by extension & size #46591

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
Jul 22, 2022
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
32 changes: 32 additions & 0 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,22 @@ public function sort(\Closure $closure): static
return $this;
}

/**
* Sorts files and directories by extension.
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @return $this
*
* @see SortableIterator
*/
public function sortByExtension(): static
{
$this->sort = Iterator\SortableIterator::SORT_BY_EXTENSION;

return $this;
}

/**
* Sorts files and directories by name.
*
Expand Down Expand Up @@ -456,6 +472,22 @@ public function sortByCaseInsensitiveName(bool $useNaturalSort = false): static
return $this;
}

/**
* Sorts files and directories by size.
*
* This can be slow as all the matching files and directories must be retrieved for comparison.
*
* @return $this
*
* @see SortableIterator
*/
public function sortBySize(): static
{
$this->sort = Iterator\SortableIterator::SORT_BY_SIZE;

return $this;
}

/**
* Sorts files and directories by type (directories before files), then by name.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Finder/Iterator/SortableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SortableIterator implements \IteratorAggregate
public const SORT_BY_NAME_NATURAL = 6;
public const SORT_BY_NAME_CASE_INSENSITIVE = 7;
public const SORT_BY_NAME_NATURAL_CASE_INSENSITIVE = 8;
public const SORT_BY_EXTENSION = 9;
public const SORT_BY_SIZE = 10;

/** @var \Traversable<string, \SplFileInfo> */
private \Traversable $iterator;
Expand Down Expand Up @@ -83,6 +85,14 @@ public function __construct(\Traversable $iterator, int|callable $sort, bool $re
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * ($a->getMTime() - $b->getMTime());
};
} elseif (self::SORT_BY_EXTENSION === $sort) {
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * strnatcmp($a->getExtension(), $b->getExtension());
};
} elseif (self::SORT_BY_SIZE === $sort) {
$this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * ($a->getSize() - $b->getSize());
};
} elseif (self::SORT_BY_NONE === $sort) {
$this->sort = $order;
} elseif (\is_callable($sort)) {
Expand Down