Следуя совету @Blindy, я реализовал этот вид с помощью PHP. Вот две функции, которые, кажется, решают эту проблему относительно легко.
protected function _sort_helper(&$input, &$output, $parent_id) {
foreach ($input as $key => $item)
if ($item->parent_id == $parent_id) {
$output[] = $item;
unset($input[$key]);
// Sort nested!!
$this->_sort_helper(&$input, &$output, $item->id);
}
}
protected function sort_items_into_tree($items) {
$tree = array();
$this->_sort_helper(&$items, &$tree, null);
return $tree;
}
Мне было бы интересно услышать, если есть более простой подход, но это, кажется, работает.