Example:
$obj1 = new stdClass();
$obj1->name = 'a';
$obj1->description = '1';
$obj2 = new stdClass();
$obj2->name = 'c';
$obj2->description = '2';
$obj3 = new stdClass();
$obj3->name = 'b';
$obj3->description = '3';
$array = array($obj1, $obj2, $obj3);
$sorted = Arrays::orderBy($array, 'name');
Result:
Array
(
[0] => stdClass Object
(
[name] => a
[description] => 1
)
[1] => stdClass Object
(
[name] => b
[description] => 3
)
[2] => stdClass Object
(
[name] => c
[description] => 2
)
)
/** * @test */ public function shouldSortArrayByField() { //given $product1 = new Product(array('id_category' => '2')); $product2 = new Product(array('id_category' => '3')); $product3 = new Product(array('id_category' => '1')); $array = array($product1, $product2, $product3); //when $sorted = Arrays::orderBy($array, 'id_category'); //then $this->assertEquals(array($product3, $product1, $product2), $sorted); }