I have an array in $arr1
, i expected to sort with some criterias (indexes) :
- distance = desc
- age = asc
- saw = desc
$arr1 = [
[
'distance' => 10,
'age' => 3,
'saw' => 2,
],
[
'distance' => 10,
'age' => 3,
'saw' => 3,
],
[
'distance' => 10,
'age' => 1,
'saw' => 1,
]
];
this problem something like nested sorting, first sorting by points (by desc) but when the points are equals then next sort to age criteria (by asc), if ages are equals then sort to saw (by desc).
I expected output something like this :
[
'distance' => 10,
'age' => 1,
'saw' => 1,
],
[
'distance' => 10,
'age' => 3,
'saw' => 2,
],
[
'distance' => 10,
'age' => 3,
'saw' => 3,
]
I try with usort but the output is messed up
usort($array, function ($a, $b) {
if($a['distance'] == $b['distance']) {
if ($a['age'] - $b['age']== 0) {
return $a['saw'] - $b['saw'];
}
return $a['age'] - $b['age'];
}
return ($a['distance'] < $b['distance']) ? 1 : -1;
});
Did I miss something ?