7

I need to sort a collection based on "sort_order" field instead of "id" as shown in the image. enter image description here

$collection=Mage::getResourceModel('banners/manage_collection');
$collection->getSelect()->order('sort_order','DESC');

And

I have tried this but its always returns collection based on id field.

20

You can add this code in the __construct method of your grid block after calling parent::__construct();:

$this->setDefaultSort('sort_order');
$this->setDefaultDir('ASC'); 

EDIT:
For frontend you can use this:

$collection = Mage::getResourceModel('banners/manage_collection');
$collection->setOrder('sort_order','DESC');
  • I need to display the items in the front end based on the sort order.I am not sure above code will work? – blakcaps Nov 27 '13 at 8:54
  • 2
    @blakcaps. Then maybe you should state that in the question. You posted a screenshot of the backend so I assumed you want the default sorting in the backend by sort_order – Marius Nov 27 '13 at 9:08
  • @Marius default sorting happens based on one column. In my case first priority goes to status and the modified date. If there are 5 active and 5 inactive columns all 5 active records has to come first and earlier modified should come first within this 5, then 5 inactive latest modified first within 5. How can I do that? – Butterfly Feb 4 '19 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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