Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Best way to reverse a collection

What is the best way to reverse a custom collection created with new Varien_Data_Collection()?

like image 813
ThreeCheeseHigh Avatar asked Sep 12 '12 09:09

ThreeCheeseHigh


Video Answer


1 Answers

Is the collection already loaded? The class supports setOrder, so if you know how it's ordered and it isn't yet loaded yet, you can simply call setOrder on the same field, using Varien_Data_Collection::SORT_ORDER_DESC or Varien_Data_Collection::SORT_ORDER_ASC as appropriate. Alternatively if you only require the objects and nothing specific to the collection you could use.

// where $collection is your instance of Varien_Data_Collection
$reversedCollection = array_reverse($collection->toArray());
like image 77
Peter O'Callaghan Avatar answered Sep 19 '22 22:09

Peter O'Callaghan