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.

1 Answer 1

21

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');
3
  • 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, 2013 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, 2013 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?
    – Anto S
    Feb 4, 2019 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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