Yii2 Gridview : how to sort on multiple columns?
<p>Say I have a number of colored balls created at different times that I wish to display in a GridView.</p>

<ul>
<li>Ball - id, color, created_at</li>
</ul>

<p>Populated as:</p>

<pre><code>1, red   , 2010-10-09
2, blue  , 2010-11-08
3, blue  , 2010-09-01
4, red   , 2010-06-15 
</code></pre>

<p>As a default I want the balls to be displayed newest first but I also want to be able to order the balls by color, so I set up the following search function in the BallSearch model:</p>

<pre><code>public function search()
{
    $query = Ball::find();

    $dataProvider = new ActiveDataProvider([
        'query' =&gt; $query,
        'sort' =&gt; [
            'attributes' =&gt; [
                'color',
                'created_at',
            ],
            'defaultOrder'=&gt; ['created_at' =&gt; SORT_DESC]
        ],
    ]);

    return $dataProvider;
}
</code></pre>

<p>This works up to a point but when sorting by color, I understandably lose the newest first sorting. I'd really like this result:</p>

<pre><code>2, blue  , 2010-11-08
3, blue  , 2010-09-01
1, red   , 2010-10-09
4, red   , 2010-06-15 
</code></pre>

<p>Unfortunately, if I put the newest first condition on the find query:</p>

<pre><code>...
$query = Ball::find()-&gt;orderBy(['created_at' =&gt; SORT_DESC]);
...
</code></pre>

<p>It gets applied before the sort ordering and the results are no longer ordered by color primarily and the GridView ordering is ineffective.</p>

<p>Is there anyway to specify this underlying secondary ordering without concatting complicated conditions to each attribute order?</p>

<p>It seems to me that this is a common use case. </p>