function setOrder(array $orderedNames, $parentName = null) {
    if (isset($parentName)) {
      if (!isset($this->children[$parentName])) {
        throw new \Exception("Unknown name '$parentName'.");
      }
      $this->children[$parentName] = $this->internalSetOrder(
        $this->children[$parentName],
        $orderedNames);
    }
    else {
      $this->toplevelNames = $this->internalSetOrder(
        $this->toplevelNames,
        $orderedNames);
    }
    return $this;
  }