function sort()
    {
        $rootNodes = $this->getRootNodes();

        $sorted = array();
        while ( count( $this->nodes ) > 0 ) {
            // check for circular reference
            if ( count( $rootNodes ) === 0 )
                return false;

            // remove this node from rootNodes and add it to the output
            $current = array_shift( $rootNodes );
            $sorted[] = $current->name;

            // for each of its children queue the new node and remove the original
            while ( $child = $current->popChild() )
            {
                $child->unregisterParent( $this->nodes[$current->name] );
                // if this child has no more parents, add it to the root nodes list
                if ( $child->parentCount() === 0 )
                    $rootNodes[] = $child;
            }

            unset( $this->nodes[$current->name] );
        }
        return $sorted;
    }