Magento grid column sorting

Magento grid column sorting

October 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento grid column sorting

Magento Grid Column Sorting: A Comprehensive Solution

Hello developers! Today, I want to address a common challenge I’ve seen many of you face - getting your custom grid columns in Magento to sort correctly. Let’s dive into the nitty-gritty details and ensure your grid behaves as expected.

Understanding the Problem

You’ve created your XML structure meticulously. You’ve built controllers and even crafted a custom grid controller, yet clicking on the column headers feels like tapping a brick wall - nothing happens! Frustrating, isn’t it? Don’t worry; we’ll get this sorted out (pun intended).

The Core Issue: Missing Sortable Flag

The key to unlocking sortable columns lies in setting the ‘sortable’ flag correctly. By default, Magento sets all columns as sortable. However, when you create a custom column and don’t specify its sortable status, it remains unsortable.

Let’s take a closer look at your code:

protected function _prepareColumns()
{
    // ... existing code ...

    $this->addColumn('id', array(
        'header' => Mage::helper('googlemerchant')->__('ID'),
        // other parameters...
        'sortable' => true,  // Add this line
    ));

    // ... rest of the columns...

    return parent::_prepareColumns();
}

As you can see, we’ve added 'sortable' => true to the column definition. This simple addition tells Magento that the ‘id’ column should indeed be sortable.

The Magic Behind the Scenes

When you set the sortable flag to true, Magento understands that this column is eligible for sorting operations. Here’s what happens under the hood:

  1. When the grid loads, Magento automatically generates sorting links for every ‘sortable’ column header.
  2. If a user clicks on a sortable column header, Magento captures the click event and updates the URL parameters accordingly (e.g., adding dir=asc or dir=desc to the query string).
  3. On refresh, Magento reads these URL parameters and sorts the collection using the specified column and direction.

A Real-life Example: Sorting by Product Name

Let’s modify your code to make the ‘product_name’ column sortable:

$this->addColumn('product_name', array(
    // ... existing parameters ...
    'sortable' => true,
));

Now, when you load the grid, clicking on the “Product Name” header will trigger sorting. The collection will be reordered based on the product names, and the grid will display the sorted results.

Sorting with Custom Collections

Your current code attempts to add custom filtering conditions before sorting the collection:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addStoreFilter($storeId)
    // ... existing filters ...

    ->addAttributeToSort('name', 'asc');

While this seems logical, it might not work as expected because Magento reads and processes sort conditions before applying custom filters. To ensure the correct order of operations, you should apply the sort condition after adding all filter conditions:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addStoreFilter($storeId)
    // ... existing filters ...

    ->addAttributeToSelect(array('enable_googlemerchant', 'name', 'entity_id', 'type_id', 'status'))
    ->addFieldToFilter('enable_googlemerchant', array("eq" => '1'))
    ->addFieldToFilter('status', array("eq" => '1'))
    // ... other filters ...

    ->setOrder('name', 'asc');

Conclusion: Sorted and Ready for Action

In this post, we’ve explored the intricacies of Magento grid sorting. By carefully setting the sortable flag and managing the order of collection manipulation methods, you can now enjoy fully functional, sortable custom columns.

Remember, proper grid configuration is key to a smooth user experience in your Magento admin panel. Don’t hesitate to experiment with different sorting parameters and filters to make your grids as intuitive and efficient as possible.

Until next time, keep coding with confidence! If you’d like to explore more Magento development tips and best practices, feel free to visit Laravel Company. We’re always eager to share our insights and help you master the Magento ecosystem.

Database API Admin Configuration

Our network