108

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']
]);
rob006
  • 21,383
  • 5
  • 53
  • 74
Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70

10 Answers10

198

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

rob006
  • 21,383
  • 5
  • 53
  • 74
Alex
  • 8,055
  • 7
  • 39
  • 61
42

Or

       $dataProvider->setSort([
        'defaultOrder' => ['topic_order'=>SORT_DESC],
        'attributes' => [...
Kristīne Glode
  • 1,409
  • 4
  • 16
  • 22
18

defaultOrder contain a array where key is a column name and value is a SORT_DESC or SORT_ASC that's why below code not working.

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

Correct Way

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

Note: If a query already specifies the orderBy clause, the new ordering instructions given by end users (through the sort configuration) will be appended to the existing orderBy clause. Any existing limit and offset clauses will be overwritten by the pagination request from end users (through the pagination configuration).

You can detail learn from Yii2 Guide of Data Provider

Sorting By passing Sort object in query

 $sort = new Sort([
        'attributes' => [
            'age',
            'name' => [
                'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Name',
            ],
        ],
    ]);

    $models = Article::find()
        ->where(['status' => 1])
        ->orderBy($sort->orders)
        ->all();
user229044
  • 232,980
  • 40
  • 330
  • 338
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
13

if you have CRUD (index) and you need set default sorting your controller for GridView, or ListView, or more... Example

public function actionIndex()
{
    $searchModel = new NewsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    // set default sorting
    $dataProvider->sort->defaultOrder = ['id' => SORT_DESC];

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

you need add

$dataProvider->sort->defaultOrder = ['id' => SORT_DESC];
Andrey Matveev
  • 145
  • 1
  • 7
4

Try to this one

$dataProvider = new ActiveDataProvider([
    'query' => $query,
]);

$sort = $dataProvider->getSort();

$sort->defaultOrder = ['id' => SORT_ASC];

$dataProvider->setSort($sort);
user210195
  • 61
  • 1
2
$dataProvider = new ActiveDataProvider([ 
    'query' => $query, 
    'sort'=> ['defaultOrder' => ['iUserId'=>SORT_ASC]] 
]);
johannchopin
  • 13,720
  • 10
  • 55
  • 101
0

you can use sort properties like this

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

if you want to sort by two feild for examle topic_order1 and topic_order2 you can do like this

$dataProvider = new ActiveDataProvider([
 'query' => $query,
 'sort'=> ['defaultOrder' => ['topic_order1' => SORT_ASC,
'topic_order2' =>SORT_DESC]]]); 
  • 1
    Hi, your answer does not add anything new; please do not copy&paste other's work without mentioning the sources . Read help: [how to answer](https://stackoverflow.com/help/how-to-answer) – pierpy May 31 '23 at 05:10
-1

As stated in the guide, you must specify the sorting behaviors of a data provider by configuring its sort properties which correspond to the configurations for yii\data\Sort

$dataProvider = new ActiveDataProvider([
         'query' => $query,
         'sort'=> ['defaultOrder' => ['topic_order' => SORT_ASC]],
     ]);
Pather
  • 22
  • 2
  • Duplicated answer. The answer https://stackoverflow.com/a/22994046/1030070 which is from 2014 has already the same code and has been marked as the correct answer. – Néstor Jul 18 '22 at 17:49
-2

you can modify search model like this

$dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
            'defaultOrder' => ['user_id ASC, document_id ASC']
        ]
    ]);
Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
-2
    $modelProduct       =   new Product();
    $shop_id            =   (int)Yii::$app->user->identity->shop_id;

    $queryProduct       =   $modelProduct->find()
                            ->where(['product.shop_id'  => $shop_id]);


    $dataProviderProduct    =   new ActiveDataProvider([
                                    'query'         =>  $queryProduct,
                                     'pagination' => [ 'pageSize' => 10 ],
                                    'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]]
                                ]); 
Andrey Nikolov
  • 12,967
  • 3
  • 20
  • 32