110

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']
]);

10 Answers 10

200

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

Sign up to request clarification or add additional context in comments.

1 Comment

This solution works but the search in index doesn't work now.
42

Or

       $dataProvider->setSort([
        'defaultOrder' => ['topic_order'=>SORT_DESC],
        'attributes' => [...

1 Comment

It's more compatible with default GII output
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();

Comments

14

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];

1 Comment

if you want to do it in the controller without changing the SearchModel, this is the way to go. Thanks!
4

Try to this one

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

$sort = $dataProvider->getSort();

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

$dataProvider->setSort($sort);

Comments

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

Comments

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 Comment

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
-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]],
     ]);

1 Comment

Duplicated answer. The answer stackoverflow.com/a/22994046/1030070 which is from 2014 has already the same code and has been marked as the correct answer.
-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]]
                                ]); 

Comments

-2

you can modify search model like this

$dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
            'defaultOrder' => ['user_id ASC, document_id ASC']
        ]
    ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.