Skip to content

Commit

Permalink
Keep original keys when using Collection->sortBy() with an array of s…
Browse files Browse the repository at this point in the history
…ort operations (#43609)

* Add test

* Use usaort instead of usort to keep the original keys
  • Loading branch information
arthurperton committed Aug 9, 2022
1 parent 14cd42d commit 7094bc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Expand Up @@ -1366,7 +1366,7 @@ protected function sortByMany(array $comparisons = [])
{
$items = $this->items;

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

Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportCollectionTest.php
Expand Up @@ -1982,6 +1982,16 @@ public function testSortByAlwaysReturnsAssoc($collection)
});

$this->assertEquals([1 => 'dayle', 0 => 'taylor'], $data->all());

$data = new $collection(['a' => ['sort' => 2], 'b' => ['sort' => 1]]);
$data = $data->sortBy([['sort', 'asc']]);

$this->assertEquals(['b' => ['sort' => 1], 'a' => ['sort' => 2]], $data->all());

$data = new $collection([['sort' => 2], ['sort' => 1]]);
$data = $data->sortBy([['sort', 'asc']]);

$this->assertEquals([1 => ['sort' => 1], 0 => ['sort' => 2]], $data->all());
}

/**
Expand Down

0 comments on commit 7094bc1

Please sign in to comment.