Skip to main content
Updating code example to work
Source Link
Brett
  • 1.9k
  • 12
  • 22

With grids in Magento, the database driver controls the sorting of data. The collection is passed the sort column and direction of sort (asc or desc) or the default is used. The data in the collection returned is output in that same order.

To change the sorting functionality in your own custom grid, you can just alter modify the _prepareCollection method to adjust the collection to what order you want.

In my custom grid, I would probably do something like this:

class My_Module_Block_Adminhtml_Module_Index_Grid extends Mage_Core_Adminhtml_Block_Widget_Grid
{
    // ...

    protected function _prepareCollection() {
        $myCollection = Mage::getModel('my_module/model')->getCollection();
        $this->setCollection($myCollection);
        parent::_prepareCollection();
    }

    // ...
    
    protected function _afterLoadCollection() {
        usort($this->_collection, array($this, '_natSortCollection'));
        return parent::_afterLoadCollection();
    }

    protected function _natSortCollection($a, $b) {
        return strnatcmp($a->getField(), $b->getField());
    }
}

Edit

See the updated code below to do what you want.

protected function _afterLoadCollection()
{
    $items = $this->_collection->getItems();
    /* To prevent duplicate items, and to not reload from the database
       we have to remove each item individually without "clear()"ing
       the collection. */
    array_walk(array_keys($items), array($this->_collection, 'removeItemByKey'));
    usort($items, array($this, '_natSortCollection'));

    /* Now add each item back to the collection in the new order */
    array_walk($items, array($this->_collection, 'addItem'));
    return parent::_afterLoadCollection();
}

protected function _natSortCollection($itemA, $itemB)
{
    return strnatcmp($itemA->getName(), $itemB->getName());
}

With grids in Magento, the database driver controls the sorting of data. The collection is passed the sort column and direction of sort (asc or desc) or the default is used. The data in the collection returned is output in that same order.

To change the sorting functionality in your own custom grid, you can just alter modify the _prepareCollection method to adjust the collection to what order you want.

In my custom grid, I would probably do something like this:

class My_Module_Block_Adminhtml_Module_Index_Grid extends Mage_Core_Adminhtml_Block_Widget_Grid
{
    // ...

    protected function _prepareCollection() {
        $myCollection = Mage::getModel('my_module/model')->getCollection();
        $this->setCollection($myCollection);
        parent::_prepareCollection();
    }

    // ...
    
    protected function _afterLoadCollection() {
        usort($this->_collection, array($this, '_natSortCollection'));
        return parent::_afterLoadCollection();
    }

    protected function _natSortCollection($a, $b) {
        return strnatcmp($a->getField(), $b->getField());
    }
}

With grids in Magento, the database driver controls the sorting of data. The collection is passed the sort column and direction of sort (asc or desc) or the default is used. The data in the collection returned is output in that same order.

To change the sorting functionality in your own custom grid, you can just alter modify the _prepareCollection method to adjust the collection to what order you want.

In my custom grid, I would probably do something like this:

class My_Module_Block_Adminhtml_Module_Index_Grid extends Mage_Core_Adminhtml_Block_Widget_Grid
{
    // ...

    protected function _prepareCollection() {
        $myCollection = Mage::getModel('my_module/model')->getCollection();
        $this->setCollection($myCollection);
        parent::_prepareCollection();
    }

    // ...
    
    protected function _afterLoadCollection() {
        usort($this->_collection, array($this, '_natSortCollection'));
        return parent::_afterLoadCollection();
    }

    protected function _natSortCollection($a, $b) {
        return strnatcmp($a->getField(), $b->getField());
    }
}

Edit

See the updated code below to do what you want.

protected function _afterLoadCollection()
{
    $items = $this->_collection->getItems();
    /* To prevent duplicate items, and to not reload from the database
       we have to remove each item individually without "clear()"ing
       the collection. */
    array_walk(array_keys($items), array($this->_collection, 'removeItemByKey'));
    usort($items, array($this, '_natSortCollection'));

    /* Now add each item back to the collection in the new order */
    array_walk($items, array($this->_collection, 'addItem'));
    return parent::_afterLoadCollection();
}

protected function _natSortCollection($itemA, $itemB)
{
    return strnatcmp($itemA->getName(), $itemB->getName());
}
Source Link
Brett
  • 1.9k
  • 12
  • 22

With grids in Magento, the database driver controls the sorting of data. The collection is passed the sort column and direction of sort (asc or desc) or the default is used. The data in the collection returned is output in that same order.

To change the sorting functionality in your own custom grid, you can just alter modify the _prepareCollection method to adjust the collection to what order you want.

In my custom grid, I would probably do something like this:

class My_Module_Block_Adminhtml_Module_Index_Grid extends Mage_Core_Adminhtml_Block_Widget_Grid
{
    // ...

    protected function _prepareCollection() {
        $myCollection = Mage::getModel('my_module/model')->getCollection();
        $this->setCollection($myCollection);
        parent::_prepareCollection();
    }

    // ...
    
    protected function _afterLoadCollection() {
        usort($this->_collection, array($this, '_natSortCollection'));
        return parent::_afterLoadCollection();
    }

    protected function _natSortCollection($a, $b) {
        return strnatcmp($a->getField(), $b->getField());
    }
}