Custom Sort In Grid View


I have a few values on a grid view and I want to sort them in a custom order. ¿How can I do it?

Lets say I have these values "A, B, C and D" and I want to sort them like this "B, C, A, D".


I think you’d use something like this in your model’s search method (assuming MySQL):

public function search()


    $criteria = new CDbCriteria;

    // search criteria here...

    $sort = new CSort;

    $sort->attributes = array(


            'asc'=>'FIELD(column_name, "B", "C", "A", "D") ASC',

            'desc'=>'FIELD(column_name, "B", "C", "A", "D") DESC',




    return new CActiveDataProvider('ClassName', array(





Thanks, it’s working. I knew how to do it with MySQL but I didn’t know how to implement it in Yii.