Magento 1.9 Grid – How to Sort Custom Renderer Column in Magento Grid

gridmagento-1.9renderer

I want to sort custom rendere column in magento grid.

See below image–

enter image description here

As in above image Sorting is done using Status column, but it is is showing wrong. I want to sort it like active,on hold,out of stock.

Here is the code in Grid.php

$this->addColumn('stock_status', array(
            'header' => Mage::helper('allstock')->__('Status'),
            'width' => '200px',
            'align' => 'center',
            'index' => 'stock_status',
            'renderer' => 'allstock/adminhtml_allstock_renderer_stockstatus'
        ));

here is the renderer code-

public function render(Varien_Object $row)
    {
        $stock_status=$row->getStockStatus();
        if($stock_status=='stock on hold')
        {
            $html="on hold";
        }
        else
        {
            $html=$stock_status;
        }
        return $html;
    }

Best Answer

Finally I got my answer after working on it a lot, Here is How I made it possible-

/* overwrite to sort stock_status column in grid*/
    protected function _setCollectionOrder($column)
    {
        $collection = $this->getCollection();
        if ($collection) {
             switch ($column->getId()) { 
                  case 'stock_status':
                    $arr = array();
                    $i=0;
                    foreach($collection as $item)  
                    { 
                        if($item->getStockStatus()=="stock on hold")  // check value of column
                        {
                            $item->setStockStatus("on hold");        // replace value of column
                        }   

                        $arr[$i] = $item;   
                        $i++    ;        
                    }

                    $collectionItems = $collection->getItems();
                    usort($collectionItems, array($this, '_sortItems'));  // re-sort collection
                    $newCollection = new Varien_Data_Collection();        // create new collection

                    foreach ($collectionItems as $item) {
                        $newCollection->addItem($item);                // assign each item to new collection
                    }
                    $this->setCollection($newCollection);

                    break;
                default:
                    parent::_setCollectionOrder($column);
                    break;
            }
        }
        return $this;
    }

    /* custom function to sort stock_status column in grid*/
    public function _sortItems($a, $b)
    {
        $columnId = "stock_status";
        $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir);

        $al = strtolower($a->getData($columnId));
        $bl = strtolower($b->getData($columnId));

        if ($al == $bl) {
            return 0;
        }

        if ($dir == 'asc') {
            return ($al < $bl) ? -1 : 1;
        } else {
            return ($al > $bl) ? -1 : 1;
        }
    } 

Following links helped me alot--

https://stackoverflow.com/questions/5673093/sort-magento-collection-after-load https://stackoverflow.com/questions/7705007/magento-grid-sorting-on-rendered-column