diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index f041f1f20849..fdfde565b89f 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1561,7 +1561,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], }; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index f4acf2c24a13..e9e0b63979ac 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -2003,6 +2003,32 @@ public function testSortByMany($collection) } #[DataProvider('collectionClassProvider')] + /** + * @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')] + /** + * @dataProvider collectionClassProvider + */ public function testSortKeys($collection) { $data = new $collection(['b' => 'dayle', 'a' => 'taylor']);