@ 2022-09-24T22:38:27Z <?php
$orderList = [
'Jake',
'Scully' => [
'pet',
'friend' => [
'Michael',
'Merissa',
],
'Norm',
],
'Amy',
'Charles',
'Hitchcock',
];
$items = [
[
'name' => 'Scully',
'related' => [
[ 'name' => 'Norm', 'category' => 'self' ],
[ 'name' => 'Earl', 'category' => 'friend' ],
[ 'name' => 'Kelly', 'category' => 'pet' ],
[ 'name' => 'Michael', 'category' => 'friend' ],
[ 'name' => 'Merissa', 'category' => 'friend' ],
],
],
[
'name' => 'Rosa',
'related' => [],
],
[
'name' => 'Jake',
'related' => [
[ 'name' => 'Franzia', 'category' => 'nemesis' ],
[ 'name' => 'Doug' ],
],
],
[
'name' => 'Hitchcock',
'related' => [],
],
[
'name' => 'Amy',
'related' => [],
],
];
function getTopLevelItemsFromMixedList(array $mixedList): array {
$topLevels = [];
foreach ($mixedList as $item => $value) {
$topLevels[] = is_int($item) ? $value : $item;
}
return $topLevels;
}
function sort_by(array $array, callable $sortKeyResolver, ...$sortArgs) {
$mapped = array_map($sortKeyResolver, $array);
asort($mapped, ...$sortArgs);
$sorted = [];
foreach ($mapped as $key => $index) {
$sorted[] = $array[$key];
}
return $sorted;
}
$topLevelOrder = getTopLevelItemsFromMixedList($orderList);
$sortKeyResolver = function ($item) use ($topLevelOrder) {
$orderListIndex = array_search($item['name'], $topLevelOrder);
return $orderListIndex === false ? count($topLevelOrder).$item['name'] : $orderListIndex;
};
// First, sort parents
$items = sort_by($items, $sortKeyResolver, SORT_NATURAL);
// Then sort children
foreach ($items as &$item) {
$children = $item['related'] ?? [];
if(empty($orderList[$item['name']])) {
// No order was specified for the children, so just sort them as normal
$item['related'] = sort_by($children, fn ($child) => $child['name'], SORT_NATURAL);
continue;
}
// An order was specified; it can contain children names or categories,
// so let'sjust call it the "Level 2" order (L2).
$l2Order = getTopLevelItemsFromMixedList($orderList[$item['name']]);
$sortKeyResolver = function ($childItem) use ($l2Order) {
// First, we check if there's an entry for the item's name
$indexOfChildInL2Order = array_search($childItem['name'], $l2Order);
if ($indexOfChildInL2Order !== false) {
return $indexOfChildInL2Order;
}
// Check if there's an entry for the item's category
$indexOfCategoryInL2Order = array_search($childItem['category'], $l2Order);
if ($indexOfCategoryInL2Order !== false) {
// There's an entry for the category!
// Now check if there's an entry for the item within the category.
$orderOfChildrenInCategory = $l2Order[$childItem['category']] ?? [];
$indexOfChildInCategory = array_search($childItem['name'], $orderOfChildrenInCategory);
return $indexOfChildInCategory === false
? $indexOfCategoryInL2Order.$childItem['name']
: ($indexOfCategoryInL2Order + ($indexOfChildInCategory * 0.1));
}
return count($l2Order).$childItem['name'];
};
$item['related'] = sort_by($children, $sortKeyResolver, SORT_NATURAL);
}
print_r($items);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.6 0.009 0.006 16.88 8.3.5 0.007 0.010 18.20 8.3.4 0.011 0.004 20.29 8.3.3 0.007 0.007 18.63 8.3.2 0.005 0.002 24.18 8.3.1 0.000 0.009 24.66 8.3.0 0.008 0.000 26.16 8.2.18 0.010 0.007 25.92 8.2.17 0.003 0.017 22.96 8.2.16 0.010 0.010 22.96 8.2.15 0.000 0.009 25.66 8.2.14 0.004 0.004 24.66 8.2.13 0.008 0.000 26.16 8.2.12 0.004 0.004 19.66 8.2.11 0.007 0.003 20.39 8.2.10 0.009 0.000 20.39 8.1.28 0.011 0.004 25.92 8.1.27 0.008 0.000 24.66 8.1.26 0.009 0.000 26.35 8.1.25 0.008 0.000 28.09 8.1.24 0.007 0.011 18.52 8.1.23 0.007 0.003 18.81 8.1.10 0.005 0.004 17.48
preferences:dark mode live preview
43.69 ms | 401 KiB | 5 Q