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

[8.x] Add missing sortByMany method to Arr helper #35277

Merged
merged 3 commits into from Nov 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Illuminate/Collections/Arr.php
Expand Up @@ -678,4 +678,45 @@ public static function wrap($value)

return is_array($value) ? $value : [$value];
}

/**
* Sort giiven array by many properties.
*
* @param array $array
* @param array $comparisons
* @return array
*/
public static function sortByMany($array, $comparisons = [])
{
usort($array, function ($a, $b) use ($comparisons) {
foreach ($comparisons as $cmp) {
// destruct comparison array to variables
// with order set by default to 1
[$prop, $ascending] = static::wrap($cmp) + [1 => true];
$result = 0;

if (is_callable($prop)) {
$result = $prop($a, $b);
} else {
$values = [static::get($a, $prop), static::get($b, $prop)];

if (! $ascending) {
$values = array_reverse($values);
}

$result = $values[0] <=> $values[1];
}

// if result is 0, values are equal
// so we have to order items by next comparison
if ($result === 0) {
continue;
}

return $result;
}
});

return $array;
}
}
53 changes: 53 additions & 0 deletions tests/Support/SupportArrTest.php
Expand Up @@ -903,4 +903,57 @@ public function testWrap()
$this->assertEquals([$obj], Arr::wrap($obj));
$this->assertSame($obj, Arr::wrap($obj)[0]);
}

public function testSortByMany()
{
$unsorted = [
['name' => 'John', 'age' => 8, 'meta' => ['key' => 3]],
['name' => 'John', 'age' => 10, 'meta' => ['key' => 5]],
['name' => 'Dave', 'age' => 10, 'meta' => ['key' => 3]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 2]],
];

// sort using keys
$sorted = array_values(Arr::sortByMany($unsorted, [
'name',
'age',
'meta.key',
]));
$this->assertEquals([
['name' => 'Dave', 'age' => 10, 'meta' => ['key' => 3]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 2]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 3]],
['name' => 'John', 'age' => 10, 'meta' => ['key' => 5]],
], $sorted);

// sort with order
$sortedWithOrder = array_values(Arr::sortByMany($unsorted, [
'name',
['age', false],
['meta.key', true],
]));
$this->assertEquals([
['name' => 'Dave', 'age' => 10, 'meta' => ['key' => 3]],
['name' => 'John', 'age' => 10, 'meta' => ['key' => 5]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 2]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 3]],
], $sortedWithOrder);

// sort using callable
$sortedWithCallable = array_values(Arr::sortByMany($unsorted, [
function ($a, $b) {
return $a['name'] <=> $b['name'];
},
function ($a, $b) {
return $b['age'] <=> $a['age'];
},
['meta.key', true],
]));
$this->assertEquals([
['name' => 'Dave', 'age' => 10, 'meta' => ['key' => 3]],
['name' => 'John', 'age' => 10, 'meta' => ['key' => 5]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 2]],
['name' => 'John', 'age' => 8, 'meta' => ['key' => 3]],
], $sortedWithCallable);
}
}