I need a method that sorts an array of items by priority.
Here is what I have working so far:
function arraySortPriority(array &$array, $offset, array $priorities)
{
uasort($array, function ($a, $b) use ($offset, $priorities) {
if (!isset($a[$offset])) {
$a[$offset] = null;
}
if (!isset($b[$offset])) {
$b[$offset] = null;
}
if ($a[$offset] == $b[$offset]) {
return 0;
}
$aPriority = isset($priorities[$a[$offset]])
? $priorities[$a[$offset]]
: null;
$bPriority = isset($priorities[$b[$offset]])
? $priorities[$b[$offset]]
: null;
return $aPriority > $bPriority ? -1 : 1;
});
}
// an array to sort
$array = [
['type' => 'A'],
['type' => 'A'],
['type' => 'B'],
['type' => 'B'],
['type' => 'C'],
['type' => 'C'],
['type' => 'D'],
['type' => 'D'],
['type' => 'E'],
['type' => 'E'],
['type' => 'F'],
['type' => 'F'],
['type' => 'G'],
['type' => 'G'],
['type' => 'H'],
['type' => 'H'],
['type' => 'Foo'],
['type' => 'Foo'],
['type' => 'Bar'],
['type' => 'Bar'],
[0 => 'no type should be last'],
[0 => 'no type should be last'],
];
// shuffle the array
shuffle($array);
// set priorities
$priorities = [
'A' => 8,
'B' => 7,
'C' => 6,
'D' => 5,
'E' => 4,
'F' => 3,
'G' => 2,
'H' => 1,
];
// call
arraySortPriority($array, 'type', $priorities);
// test output
foreach ($array as $item) {
if (isset($item['type'])) {
echo "{$item['type']}\r\n";
} else {
$values = array_values($item);
echo reset($values) . PHP_EOL;
}
}
Expected:
A
A
B
B
C
C
D
D
E
E
F
F
G
G
H
H
Foo
Foo
Bar
Bar
no type should be last
no type should be last
Actual:
A
A
B
B
C
C
D
D
E
E
F
F
G
G
H
H
no type should be last <-- should be at bottom
no type should be last <-- should be at bottom
Bar
Bar
Foo
Foo
The problem is that the items that have not been given an $offset
should be always sorted to the bottom.
This means that no type should be last
should always be sorted lower than Foo
or Bar
.
How can I do this?