Question:Which of the following code samples will get all products sorted by 'position', assuming 'position' is of a numeric type? 

A $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->addAttributeToSort('position', 'ASC'); ->load(); 

B $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->addOrder('position', 'ASC'); ->load(); 

C function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->load(); usort($products, "cmp"); 

D function mySortByPosition($a, $b) { if ($a['position'] == $b['position']) { return 0; } return ($a['position'] < $b['position']) ? -1 : 1; } $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->addSorter(mySortByPosition) ->load(); 

+ Answer
+ Report
Total Preview: 1087

Copyright © 2024. Powered by Intellect Software Ltd