How to sort array with objects by object property in Yii 2?

If you want to sort your array with objects by specific property in Yii 2 then you can use beautiful method ArrayHelper::multisort (&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR)
Example (you have unsorted array ‘history’ with objects):

Array
(
    [0] => stdClass Object
        (
            [sent_date] => 2016-01-28 13:42:10
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

    [1] => stdClass Object
        (
            [sent_date] => 2016-01-29 10:08:35
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

    [2] => stdClass Object
        (
            [sent_date] => 2016-01-31 10:08:35
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

    [3] => stdClass Object
        (
            [sent_date] => 2015-12-01 10:08:35
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

)

And you applied:

//$history[] = $object....
echo 'BEFORE:
<pre>'; print_R($history); echo '</pre>

';

ArrayHelper::multisort ( $history, 'sent_date');

echo 'AFTER:
<pre>'; print_R($history); echo '</pre>

';

Profit. Happy coding…

Results:

AFTER:

Array
(
    [0] => stdClass Object
        (
            [sent_date] => 2015-12-01 10:08:35
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

    [1] => stdClass Object
        (<a href="http://www.php.net/array-multisort" target="_blank">here</a>
            [sent_date] => 2016-01-28 13:42:10
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

    [2] => stdClass Object
        (
            [sent_date] => 2016-01-29 10:08:35
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

    [3] => stdClass Object
        (
            [sent_date] => 2016-01-31 10:08:35
            [type] => sent
            [body] => Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum
        )

)

Also you can see native PHP function array_multisort()