mfsssa
Last Updated: February 25, 2016
·
7.784K
· 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;
Say Thanks
Respond

1 Response
Add your response

14265
0 b xglw8gzaheak2z5njali 0cwsmpc2zlzwglot humeu6r4iq7awezsjfdbr5dnqagpospca63c

works! thanks

over 1 year ago ·