QueryPath\DOMQuery::sort PHP Method

sort() public method

By default, this does not change the order of the elements in the DOM. Instead, it just sorts the internal list. However, if TRUE is passed in as the second parameter then QueryPath will re-order the DOM, too.
public sort ( callback $comparator, boolean $modifyDOM = false )
$comparator callback A callback. This will be called during sorting to compare two DOMNode objects.
$modifyDOM boolean If this is TRUE, the sorted results will be inserted back into the DOM at the position of the original first element.
    public function sort($comparator, $modifyDOM = false)
    {
        // Sort as an array.
        $list = iterator_to_array($this->matches);
        if (empty($list)) {
            return $this;
        }
        $oldFirst = $list[0];
        usort($list, $comparator);
        // Copy back into SplObjectStorage.
        $found = new \SplObjectStorage();
        foreach ($list as $node) {
            $found->attach($node);
        }
        //$this->setMatches($found);
        // Do DOM modifications only if necessary.
        if ($modifyDOM) {
            $placeholder = $oldFirst->ownerDocument->createElement('_PLACEHOLDER_');
            $placeholder = $oldFirst->parentNode->insertBefore($placeholder, $oldFirst);
            $len = count($list);
            for ($i = 0; $i < $len; ++$i) {
                $node = $list[$i];
                $node = $node->parentNode->removeChild($node);
                $placeholder->parentNode->insertBefore($node, $placeholder);
            }
            $placeholder->parentNode->removeChild($placeholder);
        }
        return $this->inst($found, null, $this->options);
    }