Yii2 data provider default sorting

Sarvar Nishonboev picture Sarvar Nishonboev · Apr 10, 2014 · Viewed 105.4k times · Source

In Yii 1.1 this code works for default sorting:

$dataProvider = new CActiveDataProvider('article',array(
    'sort'=>array(
        'defaultOrder'=>'id DESC',
    ),
));

How default sorting can be set in Yii2?

Tried below code, but no result:

$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'sort' => ['defaultOrder'=>'topic_order asc']
]);

Answer

Alex picture Alex · Apr 10, 2014

I think there's proper solution. Configure the yii\data\Sort object:

 $dataProvider = new ActiveDataProvider([
     'query' => $query,
     'sort'=> ['defaultOrder' => ['topic_order' => SORT_ASC]],
 ]);

Official doc link