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] Backport #52557 - SORT_NATURAL on Collection no longer throws warning for nulls #52593

Closed
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
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ protected function sortByMany(array $comparisons = [], int $options = SORT_REGUL
$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_NATURAL => strnatcmp((string) $values[0], (string) $values[1]),
SORT_LOCALE_STRING => strcoll($values[0], $values[1]),
default => $values[0] <=> $values[1],
};
Expand Down
22 changes: 22 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,28 @@ public function testSortByMany($collection)
setlocale(LC_ALL, $defaultLocale);
}

/**
* @dataProvider collectionClassProvider
*/
public function testNaturalSortByManyWithNull($collection)
{
$itemFoo = new \stdClass();
$itemFoo->first = 'f';
$itemFoo->second = null;
$itemBar = new \stdClass();
$itemBar->first = 'f';
$itemBar->second = 's';

$data = new $collection([$itemFoo, $itemBar]);
$data = $data->sortBy([
['first', 'desc'],
['second', 'desc'],
], SORT_NATURAL);

$this->assertEquals($itemBar, $data->first());
$this->assertEquals($itemFoo, $data->skip(1)->first());
}

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