From 7098171d93653aaa4c52183f39437dbf1219986f Mon Sep 17 00:00:00 2001 From: guandeng Date: Wed, 23 Oct 2024 18:12:23 +0800 Subject: [PATCH] Fixed bug that `sortByMany` value is null when using `SORT_NATURAL` (#7124) --- src/Collection.php | 2 +- tests/CollectionTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Collection.php b/src/Collection.php index 24560c8..b36b7d6 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -1616,7 +1616,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/CollectionTest.php b/tests/CollectionTest.php index 0b20ff7..b98ae22 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1341,5 +1341,16 @@ public function testSortBy($collection) 'd' => ['id' => 4, 'name' => 'd'], 'e' => ['id' => 5, 'name' => 'e'], ]), (string) $dataMany); + + $dataManyNull = (new $collection( + [ + ['id' => 2, 'name' => 'b'], + ['id' => 1, 'name' => null], + ] + ))->sortBy([['id', 'desc'], ['name', 'desc']], SORT_NATURAL); + $this->assertEquals(json_encode([ + ['id' => 2, 'name' => 'b'], + ['id' => 1, 'name' => null], + ]), json_encode($dataManyNull)); } }