Changes
                            
    Title
    unchanged
    Searching and Sorting by Count of Related Items in CGridView
    Category
    unchanged
    How-tos
    Yii version
    unchanged
    
    Tags
    unchanged
    CGridView, search, sort, filter, STAT, relation, count
    Content
    changed
    [...]
return new CActiveDataProvider(get_class($this), array(
        'criteria' => $criteria,
        'sort' => array(
            'defaultOrder' => 't.username',
                'attributes' => array(
                
    ...
                    // order by
                
    'post_count' => array(
                        'asc' => 'post_count ASC',
                    
    'desc' => 'post_count DESC',
                    ),
                
    '*',
                ),
        
    ),
        
    'pagination' => array(
            
    'pageSize' => 20,
        
    ),
        ));
}
```
'attributes' section of sort configurations lets us overload default searching. This configuration says, that when user wants to sort by 'post_count' field it should be done like specified. The last entry '*' says that every other field of this model should be treated normally.
### CGridView
Now we have prepared everything for our grid:[...]