How are infinite paternity trees sorted?

The

data structure looks like this. {id:parentIds} id is unique, and parentIds stores the ids. of the superior. For example, if the id=9 parentIds under the id=5 under the parent class is id=1, the parentIds at the highest level is 0, so how do you sort it?

for example, the current data is:

array(
    "1"=>"0",
    "2"=>"0",
    "3"=>"0",
    "4"=>"1",
    "5"=>"1",
    "6"=>"2",
    "7"=>"1",
    "8"=>"1",
    "9"=>"2",
    "10"=>"3",
    "11"=>"3",
    "12"=>"2",
    "13"=>"1",
    "14"=>"1,8",
    "15"=>"1,8,14",
    "16"=>"2,9",
    "17"=>"1,8",
    "18"=>"3,10",
    "19"=>"3,11",
    "20"=>"3,10,18",
    "21"=>"1,8,14,15",
    "22"=>"1,8,14");

after processing (siblings may not be in order):

array(
    "1"=>"0",
    "4"=>"1",
    "5"=>"1",
    "7"=>"1",
    "8"=>"1",
    "14"=>"1,8",
    "15"=>"1,8,14",
    "22"=>"1,8,14");
    "21"=>"1,8,14,15",
    "17"=>"1,8",
    "13"=>"1",
    "2"=>"0",
    "6"=>"2",
    "9"=>"2",
    "16"=>"2,9",
    "12"=>"2",
    "3"=>"0",
    "10"=>"3",
    "18"=>"3,10",
    "20"=>"3,10,18",
    "11"=>"3",
    "19"=>"3,11",
Apr.02,2021

convert path to version number for comparison

$a = [
    '1' => '0',
    '2' => '0',
    '3' => '0',
    '4' => '1',
    '5' => '1',
    '6' => '2',
    '7' => '1',
    '8' => '1',
    '9' => '2',
    '10' => '3',
    '11' => '3',
    '12' => '2',
    '13' => '1',
    '14' => '1,8',
    '15' => '1,8,14',
    '16' => '2,9',
    '17' => '1,8',
    '18' => '3,10',
    '19' => '3,11',
    '20' => '3,10,18',
    '21' => '1,8,14,15',
    '22' => '1,8,14',
];

$result = treeSort($a);
print_r($result);

function treeSort($arr)
{
    $list = [];
    foreach ($arr as $key => $item) {
        $version = $item == 0 ? $item : ('0.' . $item);
        $list[] = [$key, str_replace(',', '.', $version) . '.' . $key];
    }
    $mySort = function ($a, $b) {
        return version_compare($a[1], $b[1], 'ge');
    };
    usort($list, $mySort);
    $tree = [];
    foreach ($list as $item) {
        $tree[$item[0]] = $arr[$item[0]];
    }
    return $tree;
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b372b7-2c09e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b372b7-2c09e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?