# binarySearch

#### binarySearch(array, itemToFind, comparator) → Number
Core/binarySearch.js 28

Finds an item in a sorted array.

Name | Type | Description
--- | --- | ---
`array` | Array | The sorted array to search.
`itemToFind` | * | The item to find in the array.
`comparator` | binarySearch~Comparator | The function to use to compare the item to elements in the array.

##### Returns:

The index of `itemToFind` in the array, if it exists. If `itemToFind` does not exist, the return value is a negative number which is the bitwise complement (~) of the index before which the itemToFind should be inserted in order to maintain the sorted order of the array.

##### Example:

    // Create a comparator function to search through an array of numbers.
    function comparator(a, b) {
        return a - b;
    };
    var numbers = [0, 2, 4, 6, 8];
    var index = Cesium.binarySearch(numbers, 6, comparator); // 3

### Type Definitions

#### Comparator(a, b) → Number
Core/binarySearch.js 56

A function used to compare two items while performing a binary search.

Name | Type | Description
--- | --- | ---
`a` | * | An item in the array.
`b` | * | The item being searched for.

##### 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) {
        return a - b;
    }