public function sort()
    {
        $index      = 0;
        $comparator = [$this->sorter, 'sort'];

        // add an index to provide stable sorting
        foreach ($this->items as &$item) {
            $item = [$index++, $item];
        }
        unset($item);

        uasort($this->items, function ($a, $b) use ($comparator) {
            $result = $comparator($a[1], $b[1]);

            // use the index to prevent undefined behaviour when comparator reports items are "equal"
            return $result === 0 ? $a[0] - $b[0] : $result;
        });

        // remove the index
        foreach ($this->items as &$item) {
            $item = $item[1];
        }
        unset($item);
    }