Skip to content
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

[10.x] Support sort option flags on sortByMany Collections #50269

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 19 additions & 4 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ public function sortDesc($options = SORT_REGULAR)
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
{
if (is_array($callback) && ! is_callable($callback)) {
return $this->sortByMany($callback);
return $this->sortByMany($callback, $options);
}

$results = [];
Expand Down Expand Up @@ -1428,13 +1428,14 @@ public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
* Sort the collection using multiple comparisons.
*
* @param array<array-key, (callable(TValue, TValue): mixed)|(callable(TValue, TKey): mixed)|string|array{string, string}> $comparisons
* @param int $options
* @return static
*/
protected function sortByMany(array $comparisons = [])
protected function sortByMany(array $comparisons = [], int $options = SORT_REGULAR)
{
$items = $this->items;

uasort($items, function ($a, $b) use ($comparisons) {
uasort($items, function ($a, $b) use ($comparisons, $options) {
foreach ($comparisons as $comparison) {
$comparison = Arr::wrap($comparison);

Expand All @@ -1452,7 +1453,21 @@ protected function sortByMany(array $comparisons = [])
$values = array_reverse($values);
}

$result = $values[0] <=> $values[1];
if (($options & SORT_FLAG_CASE) === SORT_FLAG_CASE) {
if (($options & SORT_NATURAL) === SORT_NATURAL) {
$result = strnatcasecmp($values[0], $values[1]);
} else {
$result = strcasecmp($values[0], $values[1]);
}
} else {
$result = match ($options) {
SORT_NUMERIC => intval($values[0]) <=> intval($values[1]),
SORT_STRING => strcmp($values[0], $values[1]),
SORT_NATURAL => strnatcmp($values[0], $values[1]),
SORT_LOCALE_STRING => strcoll($values[0], $values[1]),
default => $values[0] <=> $values[1],
};
}
}

if ($result === 0) {
Expand Down
88 changes: 86 additions & 2 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2031,9 +2031,9 @@ public function testSortByString($collection)
$this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));

$data = new $collection([['name' => 'taylor'], ['name' => 'dayle']]);
$data = $data->sortBy('name', SORT_STRING);
$data = $data->sortBy('name', SORT_STRING, true);

$this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
$this->assertEquals([['name' => 'taylor'], ['name' => 'dayle']], array_values($data->all()));
}

/**
Expand Down Expand Up @@ -2077,6 +2077,90 @@ public function testSortByAlwaysReturnsAssoc($collection)
$this->assertEquals([1 => ['sort' => 1], 0 => ['sort' => 2]], $data->all());
}

/**
* @dataProvider collectionClassProvider
*/
driesvints marked this conversation as resolved.
Show resolved Hide resolved
public function testSortByMany($collection)
{
$data = new $collection([['item' => '1'], ['item' => '10'], ['item' => 5], ['item' => 20]]);
$expected = $data->pluck('item')->toArray();

sort($expected);
$data = $data->sortBy(['item']);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

rsort($expected);
$data = $data->sortBy([['item', 'desc']]);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_STRING);
$data = $data->sortBy(['item'], SORT_STRING);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

rsort($expected, SORT_STRING);
$data = $data->sortBy([['item', 'desc']], SORT_STRING);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_NUMERIC);
$data = $data->sortBy(['item'], SORT_NUMERIC);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

rsort($expected, SORT_NUMERIC);
$data = $data->sortBy([['item', 'desc']], SORT_NUMERIC);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

$data = new $collection([['item' => 'img1'], ['item' => 'img101'], ['item' => 'img10'], ['item' => 'img11']]);
$expected = $data->pluck('item')->toArray();

sort($expected, SORT_NUMERIC);
$data = $data->sortBy(['item'], SORT_NUMERIC);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected);
$data = $data->sortBy(['item']);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_NATURAL);
$data = $data->sortBy(['item'], SORT_NATURAL);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

$data = new $collection([['item' => 'img1'], ['item' => 'Img101'], ['item' => 'img10'], ['item' => 'Img11']]);
$expected = $data->pluck('item')->toArray();

sort($expected);
$data = $data->sortBy(['item']);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_NATURAL | SORT_FLAG_CASE);
$data = $data->sortBy(['item'], SORT_NATURAL | SORT_FLAG_CASE);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_FLAG_CASE | SORT_STRING);
$data = $data->sortBy(['item'], SORT_FLAG_CASE | SORT_STRING);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_FLAG_CASE | SORT_NUMERIC);
$data = $data->sortBy(['item'], SORT_FLAG_CASE | SORT_NUMERIC);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

$data = new $collection([['item' => 'Österreich'], ['item' => 'Oesterreich'], ['item' => 'Zeta']]);
$expected = $data->pluck('item')->toArray();

sort($expected);
$data = $data->sortBy(['item']);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

sort($expected, SORT_LOCALE_STRING);
$data = $data->sortBy(['item'], SORT_LOCALE_STRING);
$this->assertEquals($data->pluck('item')->toArray(), $expected);

setlocale(LC_ALL, 'de_DE');

sort($expected, SORT_LOCALE_STRING);
$data = $data->sortBy(['item'], SORT_LOCALE_STRING);
$this->assertEquals($data->pluck('item')->toArray(), $expected);
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down
Loading