From 4c3f890e41b2394e62fcf6de82e8a5b2779dcdd2 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 25 Aug 2016 20:38:18 +0300 Subject: [PATCH] Arr::sort() now can take as second argument pointing in dot notation alongside with closure; --- src/Illuminate/Support/Arr.php | 6 +++--- tests/Support/SupportArrTest.php | 17 +++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index 19314186457f..bb0d1a9fe280 100755 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -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(); } diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index d70512e7e33e..dca20367ab6b 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -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()