src/Collection/ImmArray.php
protected function mergeSort(callable $cb)
    {
        $count = count($this);
        $sfa = $this->sfa;
        $result = new SplFixedArray($count);
        for ($k = 1; $k < $count; $k = $k << 1) {
            for ($left = 0; ($left + $k) < $count; $left += $k << 1) {
                $right = $left + $k;
                $rend = min($right + $k, $count);
                $m = $left;
                $i = $left;
                $j = $right;
                while ($i < $right && $j < $rend) {
                    if ($cb($sfa[$i], $sfa[$j]) <= 0) {
                        $result[$m] = $sfa[$i];
                        $i++;
                    } else {
                        $result[$m] = $sfa[$j];
                        $j++;
                    }
                    $m++;
                }
                while ($i < $right) {
                    $result[$m] = $sfa[$i];
                    $i++;
                    $m++;
                }
                while ($j < $rend) {
                    $result[$m] = $sfa[$j];
                    $j++;
                    $m++;
                }
                for ($m = $left; $m < $rend; $m++) {
                    $sfa[$m] = $result[$m];
                }
            }
        }

        return new static($sfa);
    }
protected function mergeSort(callable $cb)
    {
        $count = count($this);
        $sfa = $this->sfa;
        $result = new SplFixedArray($count);
        for ($k = 1; $k < $count; $k = $k << 1) {
            for ($left = 0; ($left + $k) < $count; $left += $k << 1) {
                $right = $left + $k;
                $rend = min($right + $k, $count);
                $m = $left;
                $i = $left;
                $j = $right;
                while ($i < $right && $j < $rend) {
                    if ($cb($sfa[$i], $sfa[$j]) <= 0) {
                        $result[$m] = $sfa[$i];
                        $i++;
                    } else {
                        $result[$m] = $sfa[$j];
                        $j++;
                    }
                    $m++;
                }
                while ($i < $right) {
                    $result[$m] = $sfa[$i];
                    $i++;
                    $m++;
                }
                while ($j < $rend) {
                    $result[$m] = $sfa[$j];
                    $j++;
                    $m++;
                }
                for ($m = $left; $m < $rend; $m++) {
                    $sfa[$m] = $result[$m];
                }
            }
        }

        return new static($sfa);
    }
protected function mergeSort ( callable $ cb ) { $ count = count ( $ this ) ; $ sfa = $ this -> sfa ; $ result = new SplFixedArray ( $ count ) ; for ( $ k = 1 ; $ k < $ count ; $ k = $ k << 1 ) { for ( $ left = 0 ; ( $ left + $ k ) < $ count ; $ left += $ k << 1 ) { $ right = $ left + $ k ; $ rend = min ( $ right + $ k , $ count ) ; $ m = $ left ; $ i = $ left ; $ j = $ right ; while ( $ i < $ right && $ j < $ rend ) { if ( $ cb ( $ sfa [ $ i ] , $ sfa [ $ j ] ) <= 0 ) { $ result [ $ m ] = $ sfa [ $ i ] ; $ i ++ ; } else { $ result [ $ m ] = $ sfa [ $ j ] ; $ j ++ ; } $ m ++ ; } while ( $ i < $ right ) { $ result [ $ m ] = $ sfa [ $ i ] ; $ i ++ ; $ m ++ ; } while ( $ j < $ rend ) { $ result [ $ m ] = $ sfa [ $ j ] ; $ j ++ ; $ m ++ ; } for ( $ m = $ left ; $ m < $ rend ; $ m ++ ) { $ sfa [ $ m ] = $ result [ $ m ] ; } } } return new static ( $ sfa ) ; }
Perform a bottom-up, non-recursive, in-place mergesort.
Efficient for very-large objects, and written without recursion
since PHP isn't well optimized for large recursion stacks.

@param callable $cb The callback for comparison
@return static
Perform a bottom - up non - recursive in - place mergesort . Efficient for very - large objects and written without recursion since PHP isn t well optimized for large recursion stacks .