public function sort_nodes() {
        $tempnodes = array();
        $this->validate_after_order();

        // First content noes.
        foreach ($this->nodes as $node) {
            $after = $node->after;
            $content = $node->content;
            if (($after == null && !empty($content)) || $node->name === 'editprofile') {
                // Can go anywhere in the cat. Also show content nodes first.
                $tempnodes = array_merge($tempnodes, array($node->name => $node), $this->find_nodes_after($node));
            }
        }

        // Now nodes with no content.
        foreach ($this->nodes as $node) {
            $after = $node->after;
            $content = $node->content;
            if ($after == null && empty($content)) {
                // Can go anywhere in the cat. Also show content nodes first.
                $tempnodes = array_merge($tempnodes, array($node->name => $node), $this->find_nodes_after($node));
            }
        }

        if (count($tempnodes) !== count($this->nodes)) {
            // Orphan nodes found.
            throw new \coding_exception('Some of the nodes specified contains invalid \'after\' property');
        }
        $this->nodes = $tempnodes;
    }