protected static function sortUnique($nodes) {
    $sort = [];
    $detached = [];
    foreach ($nodes as $node) {
      $key = $node->sortKey();
      if ($key[0] === '~') {
        $detached[] = $node;
      }
      else {
        $sort[$key] = $node;
      }
    }
    ksort($sort, SORT_NATURAL);
    return array_merge(array_values($sort), $detached);
  }