binarySearch(array, itemToFind, comparator) → Number
Finds an item in a sorted array.
Name | Type | Description |
---|---|---|
array |
Array | The sorted array to search. |
itemToFind |
Object | 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.
var comparator = function(a, b) {
return a - b;
};
var numbers = [0, 2, 4, 6, 8];
var index = Cesium.binarySearch(numbers, 6, comparator); // 3
Source:
Core/binarySearch.js, line 32
Type Definitions
-
Comparator(a, b) → Number
-
A function used to compare two items while performing a binary search.
Name Type Description a
Object An item in the array. b
Object The item being searched for. Returns:
Returns a negative value ifa
is less thanb
, a positive value ifa
is greater thanb
, or 0 ifa
is equal tob
.Example:
function compareNumbers(a, b) { return a - b; }
Source: Core/binarySearch.js, line 66