在已排序数组中查找项。
Name | Type | Description |
---|---|---|
array |
Array | 要搜索的已排序数组。 |
itemToFind |
* | 要在数组中查找的项。 |
comparator |
binarySearch~Comparator | 用于将项与之比较的函数 数组中的元素。 |
Returns:
数组中
itemToFind
的索引(如果存在)。如果是itemToFind
不存在,返回值为负数,它是位补码(~)
为了维护
数组的排序顺序。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
在执行二进制搜索时用来比较两个项目的函数。
Name | Type | Description |
---|---|---|
a |
* | 数组中的项。 |
b |
* | 正在搜索的项目。 |
Returns:
如果a小于
b
,则返回a
nega
ctive va
lue,
如果a大于b
,则为正值,或
如果a等于b
,则为0。Example:
function compareNumbers(a, b) {
return a - b;
}