Skip to content

Commit

Permalink
Arr::sort() now can take as second argument pointing in dot notation …
Browse files Browse the repository at this point in the history
…alongside with closure; (#15050)
  • Loading branch information
Syra authored and taylorotwell committed Aug 26, 2016
1 parent ed0311e commit 3dc4155
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,13 @@ public static function set(&$array, $key, $value)
}

/**
* Sort the array using the given callback.
* Sort the array using the given callback or "dot" notation.
*
* @param array $array
* @param callable $callback
* @param callable|string $callback
* @return array
*/
public static function sort($array, callable $callback)
public static function sort($array, $callback)
{
return Collection::make($array)->sortBy($callback)->all();
}
Expand Down
17 changes: 11 additions & 6 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,25 @@ public function testSet()

public function testSort()
{
$array = [
$unsorted = [
['name' => 'Desk'],
['name' => 'Chair'],
];

$array = array_values(Arr::sort($array, function ($value) {
return $value['name'];
}));

$expected = [
['name' => 'Chair'],
['name' => 'Desk'],
];
$this->assertEquals($expected, $array);

// sort with closure
$sortedWithClosure = array_values(Arr::sort($unsorted, function ($value) {
return $value['name'];
}));
$this->assertEquals($expected, $sortedWithClosure);

// sort with dot notation
$sortedWithDotNotation = array_values(Arr::sort($unsorted, 'name'));
$this->assertEquals($expected, $sortedWithDotNotation);
}

public function testSortRecursive()
Expand Down

0 comments on commit 3dc4155

Please sign in to comment.