Skip to content

Commit

Permalink
Optional callback for Arr::sort() (#21337)
Browse files Browse the repository at this point in the history
  • Loading branch information
fitztrev authored and taylorotwell committed Sep 22, 2017
1 parent 3417259 commit 9754d89
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,10 @@ public static function shuffle($array)
* Sort the array using the given callback or "dot" notation.
*
* @param array $array
* @param callable|string $callback
* @param callable|string|null $callback
* @return array
*/
public static function sort($array, $callback)
public static function sort($array, $callback = null)
{
return Collection::make($array)->sortBy($callback)->all();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ function array_set(&$array, $key, $value)
* Sort the array by the given callback or attribute name.
*
* @param array $array
* @param callable|string $callback
* @param callable|string|null $callback
* @return array
*/
function array_sort($array, $callback)
function array_sort($array, $callback = null)
{
return Arr::sort($array, $callback);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ public function testSort()
['name' => 'Desk'],
];

$sorted = array_values(Arr::sort($unsorted));
$this->assertEquals($expected, $sorted);

// sort with closure
$sortedWithClosure = array_values(Arr::sort($unsorted, function ($value) {
return $value['name'];
Expand Down

0 comments on commit 9754d89

Please sign in to comment.