3

How can I sort orders by newest? Currently I'm filtering by complete and canceled status and I want to sort results by latest ordered date or by latest order ID. Now it show to me by the first ID lets say 10000001 but i want it to be 1000010

$collection = Mage::getModel('sales/order')->getCollection() ->addFieldToFilter('status', array('nin' => array('canceled','complete'))) ->addFieldToFilter('state',Array('eq'=>Mage_Sales_Model_Order::STATE_NEW)) ->addAttributeToSelect('*'); 

1 Answer 1

3

I believe you are looking for the addAttributeToSort() method:

$collection = Mage::getModel('sales/order')->getCollection() ->addFieldToFilter('status', array('nin' => array('canceled', 'complete'))) ->addFieldToFilter('state', array('eq' => Mage_Sales_Model_Order::STATE_NEW)) ->addAttributeToSelect('*') ->addAttributeToSort('increment_id', 'DESC') ; foreach ($collection as $item) { echo $item->getIncrementId(); } 

You can edit the attribute (and direction) on which the collection has to be sorted on.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.