I have an array that contains the key counted
and placement
which I am trying to group by before I sort. The array should be first sorted by counted
and then, for each duplicate counted
, should then sort by placement
.
JavaScript
x
$array = [
[
'id' => 1,
'placement' => 8,
'counted' => 3
'user' => ['name' => 'foo'],
],
[
'id' => 2,
'placement' => 5,
'counted' => 3
'user' => ['name' => 'bar'],
],
[
'id' => 3,
'placement' => 1,
'counted' => 2
'user' => ['name' => 'foobar'],
]
];
My expected output here would be:
JavaScript
$array = [
[
'id' => 2,
'placement' => 5,
'counted' => 3
'user' => ['name' => 'bar'],
],
[
'id' => 1,
'placement' => 8,
'counted' => 3
'user' => ['name' => 'foo'],
],
[
'id' => 3,
'placement' => 1,
'counted' => 2
'user' => ['name' => 'foobar'],
]
];
I have tried to usort
to achieve this:
JavaScript
usort($array, fn($a, $b) => ((int)$a['placement'] <=> (int)$b['counted']) * -1);
But this gives me an unexpected result. Everything I try seems to not work, any ideas would be appreciated.
Advertisement
Answer
Since you prefer using usort so this is my answser
JavaScript
$array = [
[
'id' => 1,
'placement' => 8,
'counted' => 3,
'user' => ['name' => 'foo'],
],
[
'id' => 2,
'placement' => 5,
'counted' => 3,
'user' => ['name' => 'bar'],
],
[
'id' => 3,
'placement' => 1,
'counted' => 2,
'user' => ['name' => 'foobar'],
]
];
usort($array, function ($a, $b) {
if ($a['counted'] < $b['counted']) {
return 1;
}
if ($a['counted'] === $b['counted'] && $a['placement'] > $b['placement']) {
return 1;
}
});