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;
Written by Matteo Caberlotto
Related protips
1 Response
works! thanks
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Sorting
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#