To sort an existing collection, you are looking for the ArrayCollection :: getIterator () method, which returns an ArrayIterator. Example:
$iterator = $collection->getIterator(); $iterator->uasort(function ($a, $b) { return ($a->getPropery() < $b->getProperty()) ? -1 : 1; }); $collection = new ArrayCollection(iterator_to_array($iterator));
The easiest way is to allow the request in the repository to process your sort.
Imagine you have a SuperEntity with a ManyToMany relationship with Category objects.
Then, for example, by creating such a repository method:
// Vendor/YourBundle/Entity/SuperEntityRepository.php public function findByCategoryAndOrderByName($category) { return $this->createQueryBuilder('e') ->where('e.category = :category') ->setParameter('category', $category) ->orderBy('e.name', 'ASC') ->getQuery() ->getResult() ; }
... makes sorting easy.
Hope this helps.
nifr May 23 '13 at 7:08 2013-05-23 07:08
source share