mfsssa
Last Updated: February 25, 2016
·
11.38K
· teito79

Sorting Doctrine ArrayCollection

To sort a Doctrine ArrayCollection, the class offers a method to instantiate an ArrayIterator to sort elements using a closure function:

// get a new ArrayIterator
$iterator = $myCollection->getIterator();

// define ordering closure, using preferred comparison method/field
$iterator->uasort(function ($first, $second) {
    return (int) $first->getPosition() > (int) $second->getPosition() ? 1 : -1;
});

// return the ordered iterator
return $iterator;

1 Response
Add your response

14265

works! thanks

over 1 year ago ·