Sort rows of a 2d array by specific value in one column, then by values in another column

46 views Asked by At

I have an array:

[
{id: 1, status: 2, temperature:14}
{id: 2, status: 2, temperature:21}
{id: 3, status: 4, temperature:12}
{id: 4, status: 2, temperature:31} 
{id: 5, status: 1, temperature:35} 
]

I'd like to sort the array by: status = 2 first, temperature desc second.

so it needs to sort based on two "columns". The above needs to output :

[
{id: 4, status: 2, temperature:31} 
{id: 2, status: 2, temperature:21}
{id: 1, status: 2, temperature:14}
{id: 5, status: 1, temperature:35} 
{id: 3, status: 4, temperature:12}
]

DISCLAIMER: I'm on PHP version 5.6.4; I cannot use spaceship operator.

0

There are 0 answers