Programmatically Sort Product Collection in Magento

collection;product-collectionsorting

I have a product collection. How does one go about sorting this collection the Magento Way?

Best Answer

easymoden00b,Magento have a magic function addAttributeToSort() which is sort your product Collection on basic of product attribute

Format:

$ProductCollectionObject->addAttributeToSort('attribute_code', 'sort_order')

Example:

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection->addAttributeToSort('created_at', 'desc');

Here i have filter the product collection by created_at attribute

Related Topic