Reverse Sort Product Collection in Magento

product-collection

I have something like this

foreach ($this->getProductCollection()as $prod) { ?>
.....

And I want to reverse the foreach loop so that it starts with the last product in the array.

Is there a quick way to do this?

Best Answer

You should use addAttributeToSort() method of the Product Collection object before using it in a loop.

Something like that: //Correcting Misspellig, it was attAttribute $this->getProductCollection()->addAttributeToSort('attr_name', 'desc'); foreach ($this->getProductCollection() as $prod) { ... }

more about collections