# mergeSort

#### mergeSort(array, comparator, userDefinedObject)
Core/mergeSort.js 72

A stable merge sort.

Name | Type | Description
--- | --- | ---
`array` | Array | The array to sort.
`comparator` | mergeSort~Comparator | The function to use to compare elements in the array.
`userDefinedObject` | * | optional Any item to pass as the third parameter to `comparator`.

##### Example:

    // Assume array contains BoundingSpheres in world coordinates.
    // Sort them in ascending order of distance from the camera.
    var position = camera.positionWC;
    Cesium.mergeSort(array, function(a, b, position) {
        return Cesium.BoundingSphere.distanceSquaredTo(b, position) - Cesium.BoundingSphere.distanceSquaredTo(a, position);
    }, position);

### Type Definitions

#### Comparator(a, b, userDefinedObject) → Number
Core/mergeSort.js 96

A function used to compare two items while performing a merge sort.

Name | Type | Description
--- | --- | ---
`a` | * | An item in the array.
`b` | * | An item in the array.
`userDefinedObject` | * | optional An object that was passed to `mergeSort`.

##### Returns:

Returns a negative value if `a` is less than `b`, a positive value if `a` is greater than `b`, or 0 if `a` is equal to `b`.

##### Example:

    function compareNumbers(a, b, userDefinedObject) {
        return a - b;
    }