Adding a Default Order/Sort for Data in a CGridView

Last updated:

Everyone needs to sort their data, and you will too if your application is consumed by human beings (as opposed to by machines).

Most of the time you will want to set a convenient default for the benefit of your users, like names or dates.

A good way to do that in Yii is to set the $_GET variable if it isn't already set (so that users get to see your default value the first time they load the page but they can also sort the data some way else should they want it). Just add this to your rules() function inside your model Class. Assuming you have a model class called Report and you want the default order to take into account the date in which the Report was started:


which will produce something like this:


Dialogue & Discussion