How can I append a structure entry and set its order?
If you're adding them all as new entries anyway, why not just sort the records before you create the entries?

usort($records, array($this, 'sortCategoriesByPosition'));


Add that before your foreach loop, then...

private function sortCategoriesByPosition($a, $b)
{
    if ($a-&gt;position == $b-&gt;position)
        return 0; 

    return ($a-&gt;position &lt; $b-&gt;position) ? -1 : 1;
}


Stop trying to make things harder than they should be.