Yii2网格:按计算的数值或公式列排序

I'm using GridView (generated by Gii) with ActiveRecord. Let's say I have a table with a column named "number". I added a function to the model, it's something like this:

public function getCos()
{
return cos($this->number);
}

I can't see how should I sort columns like this one, with numeric calculated values which are not just the concatenation of two columns, like the case described here:

https://www.yiiframework.com/wiki/621/filter-sort-by-calculatedrelated-fields-in-gridview-yii-2-0

Values could be calculated in the view instead of model:

'value'=>function($model, $key, $index, $widget){return cos($model->number);

But the problem is the same, I can't see how to sort this kind of column.

Thank you.

Follow the guide you mentioned but add an attribute to the dataprovider's sort like this:

'the_cosine' => [
    'asc' => 'COS(number) ASC',
    'desc' => 'COS(number) DESC',
]

That allows you to sort on a database level.