Skip to content

Commit

Permalink
Fixed bug that sortByMany value is null when using SORT_NATURAL (…
Browse files Browse the repository at this point in the history
…#7124)
  • Loading branch information
guandeng authored Oct 23, 2024
1 parent 492fa81 commit 7098171
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
};
Expand Down
11 changes: 11 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 7098171

Please sign in to comment.