This doesn't seem to be ordered correctly, anything i'm doing wrong? Suggestions?

        $componentQuantityCollection = Mage::getModel('catalog/product')->getCollection();
        $componentQuantityCollection->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left');
        $componentQuantityCollection->addAttributeToFilter('sku', array('in' => $componentSkus))->setOrder('sku','ASC');

Another collection that doesn't seem to be sorted which is different than the first:

        $kitCollection = Mage::getModel('kitinventory/kitinventory')
            ->getCollection()
            ->addFieldToFilter('kit_sku', $sku)
            ->setOrder('related_sku', 'DESC');
up vote 38 down vote accepted

EAV collections work with attributes, the sorting method is a little different here as well

$componentQuantityCollection->addAttributeToSort('sku', 'ASC');

For non-EAV collections use one of the following methods

$kitCollection->getSelect()->order('related_sku DESC');
$kitCollection->setOrder('related_sku', 'DESC');
  • what about the second collection? – easymoden00b Mar 26 '15 at 15:21
  • That's a flat type collection, so no EAV and attributes. Take a look at this answer on how to sort that: stackoverflow.com/a/11354060 – Sander Mangel Mar 26 '15 at 15:23
  • I tried setOrder('related_sku','DESC'); but it's not sorted. – easymoden00b Mar 26 '15 at 15:26
  • I've edited my answer – Sander Mangel Mar 26 '15 at 15:28
  • 2
    @easymoden00b use this $kitCollection->getSelect()->order('related_sku DESC'); – Priyank Feb 28 '17 at 13:10

You can add sort order like this:

$kitCollection->getSelect()->order('related_sku DESC');

More information: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

Hope can help you.

  • 3
    Fixed it for me, because i was using it like ->order('related_sku', 'desc'); – Ricky Odin Matthews Sep 25 '15 at 15:05

To expand on the other answers here, $kitCollection->getSelect()->order('column DESC') works fine, but you can't add more than one column. For instance, $kitCollection->getSelect()->order('column DESC, column2 ASC') will error. This is because of the work that Magento does to escape the column names. To get around this, you can use a Zend_Db_Expr like so:

$kitCollection->getSelect()->order(new Zend_Db_Expr('related_sku DESC, column2 ASC'));

easymoden00b,setOrder() is not working due Eav structure on product.As @Sande say to use addAttributeToSort()function,because of

  • Magento is join multiple tables for product collection.

  • Attribute alias name at collection

  • setOrder() function is working when it order expression Fieldname,SortOrder is correct.

You can see,how magento is create field alias and it relate eav table attribute at class Mage_Eav_Model_Entity_Collection_Abstract

public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
    if (isset($this->_joinFields[$attribute])) {
        $this->getSelect()->order($this->_getAttributeFieldName($attribute).' '.$dir);
        return $this;
    }
    if (isset($this->_staticFields[$attribute])) {
        $this->getSelect()->order("e.{$attribute} {$dir}");
        return $this;
    }
    if (isset($this->_joinAttributes[$attribute])) {
        $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
        $entityField = $this->_getAttributeTableAlias($attribute) . '.' . $attrInstance->getAttributeCode();
    } else {
        $attrInstance = $this->getEntity()->getAttribute($attribute);
        $entityField = 'e.' . $attribute;
    }

    if ($attrInstance) {
        if ($attrInstance->getBackend()->isStatic()) {
            $orderExpr = $entityField;
        } else {
            $this->_addAttributeJoin($attribute, 'left');
            if (isset($this->_joinAttributes[$attribute])||isset($this->_joinFields[$attribute])) {
                $orderExpr = $attribute;
            } else {
                $orderExpr = $this->_getAttributeTableAlias($attribute).'.value';
            }
        }

        if (in_array($attrInstance->getFrontendClass(), $this->_castToIntMap)) {
            $orderExpr = Mage::getResourceHelper('eav')->getCastToIntExpression(
                $this->_prepareOrderExpression($orderExpr)
            );
        }

        $orderExpr .= ' ' . $dir;
        $this->getSelect()->order($orderExpr);
    }
    return $this;
}

Here is my solution for sorting the order of options in a configurable product's attribute. Start by copying Collection.php (app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php) to app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php. Then you can find this code:

foreach ($this->getProduct()->getTypeInstance(true)
    ->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct())
as $associatedProduct) {

And replace it with this code:

$assProds = $this->getProduct()->getTypeInstance(true)
    ->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct());
sort($assProds);
foreach ($assProds as $associatedProduct) {

This will allow you to sort the drop down list of attribute options to alphabetical. You can also reverse the order by using array_reverse() or similar functions.

Previously, my attribute options were in reverse alphabetical order. Now, they're in alphabetical order.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.