Magento – How to sort special price collection

collection;magento-1.7

I have a page with promotions based on set special price from/to date attribute and it shos fine. I have created a new attribute called flash that is a yes/no value. I want to order the previous collection so I show first the ones that have such attribute as a Yes. This is my code and it doesn't sort, the sql query doesn't have a order by clause. It doesn't work replacing setOrder with addAttributeToSort either.

    $collection = Mage::getResourceModel('catalog/product_collection');
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
        ->addAttributeToFilter('special_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $todayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->setOrder('flash', 'ASC')
        ->setPageSize($this->getProductsCount())
        ->setCurPage(1)

    ;

Best Answer

Have a look on the query to fix this issue.

die((string)$collection->getSelect());

and check whether the ORDER BY clause is there. The code is correct from my perspective.

Works

   $collection = Mage::getResourceModel('catalog/product_collection');
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $collection->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addUrlRewrite()
        ->addStoreFilter()
        ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => now()))
        ->addAttributeToFilter('special_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => now()),
                1 => array('is' => new Zend_Db_Expr('null')))
                                                  ), 'left')
        ->addAttributeToSort('name')
        ->setPageSize(10)
        ->setCurPage(1)

    ;
    die((string)$collection->getSelect());

I have a ORDER BY clause. Did you refresh indexes and caches? I don't think this is the problem, but might be. I have no attribute flash here, so I just tried with name and this works.