Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11be2be

Browse files
committed
added array_sort helper that is short-cut into collection sortBy.
1 parent ec9d422 commit 11be2be

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/Illuminate/Support/helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ function array_set(&$array, $key, $value)
341341
}
342342
}
343343

344+
if ( ! function_exists('array_sort'))
345+
{
346+
/**
347+
* Sort the array using the given Closure.
348+
*
349+
* @param array $array
350+
* @param \Closure $callback
351+
* @return array
352+
*/
353+
function array_sort($array, Closure $callback)
354+
{
355+
return Illuminate\Support\Collection::make($array)->sortBy($callback)->all();
356+
}
357+
}
358+
344359
if ( ! function_exists('asset'))
345360
{
346361
/**

tests/Support/SupportHelpersTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,20 @@ public function testObjectGet()
197197
$this->assertEquals('Taylor', object_get($class, 'name.first'));
198198
}
199199

200+
201+
public function testArraySort()
202+
{
203+
$array = array(
204+
array('name' => 'baz'),
205+
array('name' => 'foo'),
206+
array('name' => 'bar'),
207+
);
208+
209+
$this->assertEquals(array(
210+
array('name' => 'bar'),
211+
array('name' => 'baz'),
212+
array('name' => 'foo')),
213+
array_values(array_sort($array, function($v) { return $v['name']; })));
214+
}
215+
200216
}

0 commit comments

Comments
 (0)