Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Laravel 5.5.9 changes (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Sep 20, 2017
1 parent 15b6e70 commit e277d19
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,24 +1340,27 @@ public function sort(callable $callback = null)
*/
public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
{
$results = [];
list($values, $results) = [[], []];

$callback = $this->valueRetriever($callback);

// First we will loop through the items and get the comparator from a callback
// 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);
$values[] = $callback($value, $key);
}

$descending ? arsort($results, $options)
: asort($results, $options);
$keys = array_keys($this->items);

$order = $descending ? SORT_DESC : SORT_ASC;

array_multisort($values, $order, $options, $keys, $order);

// Once we have sorted all of the keys in the array, we will loop through them
// and grab the corresponding model so we can set the underlying items list
// to the sorted version. Then we'll just return the collection instance.
foreach (array_keys($results) as $key) {
foreach ($keys as $key) {
$results[$key] = $this->items[$key];
}

Expand Down
41 changes: 40 additions & 1 deletion tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ public function testSortBy()
public function testSortByString()
{
$data = new Collection([['name' => 'taylor'], ['name' => 'dayle']]);
$data = $data->sortBy('name');
$data = $data->sortBy('name', SORT_STRING);

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

Expand All @@ -805,6 +805,45 @@ public function testSortByString()
$this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($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 testSortByStringMaintainsOriginalOrderOfItemsWithIdenticalValues()
{
$data = new Collection([['name' => 'taylor'], ['name' => 'dayle'], ['name' => 'dayle']]);

$data = $data->sortBy('name', SORT_STRING);

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

public function testSortByNumericMaintainsOriginalOrderOfItemsWithIdenticalValues()
{
$data = new Collection([['name' => '2'], ['name' => '1'], ['name' => '1']]);
$data = $data->sortBy('name', SORT_NUMERIC);

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

public function testSortByNaturalMaintainsOriginalOrderOfItemsWithIdenticalValues()
{
$data = new Collection([['name' => 'img10.png'], ['name' => 'img1.png'], ['name' => 'img1.png']]);

$data = $data->sortBy('name', SORT_NATURAL);

$this->assertEquals([['name' => 'img1.png'], ['name' => 'img1.png'], ['name' => 'img10.png']], array_values($data->all()));
$this->assertEquals([1, 2, 0], $data->keys()->all());
}

public function testSortByAlwaysReturnsAssoc()
{
$data = new Collection(['a' => 'taylor', 'b' => 'dayle']);
Expand Down

0 comments on commit e277d19

Please sign in to comment.