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

[5.5] Maintain original order of collection items with equal values when using sortBy() #21214

Merged
merged 1 commit into from
Sep 17, 2017
Merged
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/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
// function which we were given. Then, we will sort the returned values and
// and grab the corresponding values for the sorted keys from this array.
foreach ($this->items as $key => $value) {
$results[$key] = $callback($value, $key);
$results[$key] = [$callback($value, $key), $key];
}

$descending ? arsort($results, $options)
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,15 @@ public function testSortByAlwaysReturnsAssoc()
$this->assertEquals([1 => 'dayle', 0 => 'taylor'], $data->all());
}

public function testSortByMaintainsOriginalOrderOfItemsWithIdenticalValues()
{
$data = new Collection([['name' => 'taylor'], ['name' => 'dayle'], ['name' => 'dayle']]);
$data = $data->sortBy('name');

$this->assertEquals([['name' => 'dayle'], ['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
$this->assertEquals([1, 2, 0], $data->keys()->all());
}

public function testReverse()
{
$data = new Collection(['zaeed', 'alan']);
Expand Down