system/classes/Collection.php
protected function mergesort(&$array, $comparisonFunction) {

        // Exit right away if only zero or one item.
        if (count($array) < 2) {
            return true;
        }

        // Cut results in half.
        $halfway    = count($array) / 2;
        $leftArray  = array_slice($array, 0, $halfway, true);
        $rightArray = array_slice($array, $halfway, null, true);

        // Recursively call sort on left and right pieces
        $this->mergesort($leftArray, $comparisonFunction);
        $this->mergesort($rightArray, $comparisonFunction);

        // Check if the last element of the first array is less than the first element of 2nd.
        // If so, we are done. Just put the two arrays together for final result.
        if (call_user_func($comparisonFunction, end($leftArray), reset($rightArray)) < 1) {
            $array = $leftArray + $rightArray;
            return true;
        }

        // Set result array to blank. Set pointers to beginning of pieces.
        $array = array();
        reset($leftArray);
        reset($rightArray);

        // While looking at the current element in each array...
        while (current($leftArray) && current($rightArray)) {

            // Add the lowest element between the current element in the left and right arrays to the result.
            // Then advance to the next item on that side.
            if (call_user_func($comparisonFunction, current($leftArray), current($rightArray)) < 1) {
                $array[key($leftArray)] = current($leftArray);
                next($leftArray);
            } else {
                $array[key($rightArray)] = current($rightArray);
                next($rightArray);
            }
        }

        // After doing the left and right comparisons above, you may hit the end of the left array
        // before hitting the end of the right (or vice-versa). We need to make sure these left-over
        // elements get added to our results.
        while (current($leftArray)) {
            $array[key($leftArray)] = current($leftArray);
            next($leftArray);
        }
        while (current($rightArray)) {
            $array[key($rightArray)] = current($rightArray);
            next($rightArray);
        }
        return true;
    }
protected function mergesort(&$array, $comparisonFunction) {

        // Exit right away if only zero or one item.
        if (count($array) < 2) {
            return true;
        }

        // Cut results in half.
        $halfway    = count($array) / 2;
        $leftArray  = array_slice($array, 0, $halfway, true);
        $rightArray = array_slice($array, $halfway, null, true);

        // Recursively call sort on left and right pieces
        $this->mergesort($leftArray, $comparisonFunction);
        $this->mergesort($rightArray, $comparisonFunction);

        // Check if the last element of the first array is less than the first element of 2nd.
        // If so, we are done. Just put the two arrays together for final result.
        if (call_user_func($comparisonFunction, end($leftArray), reset($rightArray)) < 1) {
            $array = $leftArray + $rightArray;
            return true;
        }

        // Set result array to blank. Set pointers to beginning of pieces.
        $array = array();
        reset($leftArray);
        reset($rightArray);

        // While looking at the current element in each array...
        while (current($leftArray) && current($rightArray)) {

            // Add the lowest element between the current element in the left and right arrays to the result.
            // Then advance to the next item on that side.
            if (call_user_func($comparisonFunction, current($leftArray), current($rightArray)) < 1) {
                $array[key($leftArray)] = current($leftArray);
                next($leftArray);
            } else {
                $array[key($rightArray)] = current($rightArray);
                next($rightArray);
            }
        }

        // After doing the left and right comparisons above, you may hit the end of the left array
        // before hitting the end of the right (or vice-versa). We need to make sure these left-over
        // elements get added to our results.
        while (current($leftArray)) {
            $array[key($leftArray)] = current($leftArray);
            next($leftArray);
        }
        while (current($rightArray)) {
            $array[key($rightArray)] = current($rightArray);
            next($rightArray);
        }
        return true;
    }
protected function mergesort ( & $ array , $ comparisonFunction ) { // Exit right away if only zero or one item. if ( count ( $ array ) < 2 ) { return true ; } // Cut results in half. $ halfway = count ( $ array ) / 2 ; $ leftArray = array_slice ( $ array , 0 , $ halfway , true ) ; $ rightArray = array_slice ( $ array , $ halfway , null , true ) ; // Recursively call sort on left and right pieces $ this -> mergesort ( $ leftArray , $ comparisonFunction ) ; $ this -> mergesort ( $ rightArray , $ comparisonFunction ) ; // Check if the last element of the first array is less than the first element of 2nd. // If so, we are done. Just put the two arrays together for final result. if ( call_user_func ( $ comparisonFunction , end ( $ leftArray ) , reset ( $ rightArray ) ) < 1 ) { $ array = $ leftArray + $ rightArray ; return true ; } // Set result array to blank. Set pointers to beginning of pieces. $ array = array ( ) ; reset ( $ leftArray ) ; reset ( $ rightArray ) ; // While looking at the current element in each array... while ( current ( $ leftArray ) && current ( $ rightArray ) ) { // Add the lowest element between the current element in the left and right arrays to the result. // Then advance to the next item on that side. if ( call_user_func ( $ comparisonFunction , current ( $ leftArray ) , current ( $ rightArray ) ) < 1 ) { $ array [ key ( $ leftArray ) ] = current ( $ leftArray ) ; next ( $ leftArray ) ; } else { $ array [ key ( $ rightArray ) ] = current ( $ rightArray ) ; next ( $ rightArray ) ; } } // After doing the left and right comparisons above, you may hit the end of the left array // before hitting the end of the right (or vice-versa). We need to make sure these left-over // elements get added to our results. while ( current ( $ leftArray ) ) { $ array [ key ( $ leftArray ) ] = current ( $ leftArray ) ; next ( $ leftArray ) ; } while ( current ( $ rightArray ) ) { $ array [ key ( $ rightArray ) ] = current ( $ rightArray ) ; next ( $ rightArray ) ; } return true ; }
A stable implementation of Mergesort (aka Stable-sort).
The end result is the array passed in being sorted according to the strategy provided
by the comparison function passed in.

@param $array Array
@param $comparisionFunction A Callable.
A stable implementation of Mergesort ( aka Stable - sort ) . The end result is the array passed in being sorted according to the strategy provided by the comparison function passed in .