public function sort()
{
// Separate the values into buckets to sort them separately
$buckets = ['index' => [], 'numeric' => [], 'normal' => [], 'down_numeric' => [], 'down' => []];
foreach ($this->children as $key => $entry) {
$name = $entry->getName();
if ($name == 'index' || $name == '_index') {
$buckets['index'][$key] = $entry;
continue;
}
if ($name[0] == '-') {
if (is_numeric($name[1])) {
$exploded = explode('_', $name);
$buckets['down_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
continue;
}
$buckets['down'][$key] = $entry;
continue;
}
if (is_numeric($name[0])) {
$exploded = explode('_', $name);
$buckets['numeric'][abs($exploded[0])][$key] = $entry;
continue;
}
$buckets['normal'][$key] = $entry;
}
$final = [];
foreach ($buckets as $name => $bucket) {
if ($name == 'numeric' || $name == 'down_numeric') {
ksort($bucket);
foreach ($bucket as $sub_bucket) {
$final = $this->sortBucket($sub_bucket, $final);
}
} else {
$final = $this->sortBucket($bucket, $final);
}
}
$this->children = $final;
}