Home > rc-js-util > arrayBinaryIndexOf
Performs a bisection search of an ‘indexable’ object, i.e. can be accessed by index, for example Array
. Custom data structures are also supported.
Signature:
export declare function arrayBinaryIndexOf<T>(indexable: T, comparisonValueToSearchFor: number, getComparisonValueAtIndex: TGetComparisonValueAtIndex<T>, length: number, start?: number): number;
Parameter | Type | Description |
---|---|---|
indexable | T | The thing to be searched. This must be sorted ascending. |
comparisonValueToSearchFor | number | The comparison value which is being searched for. |
getComparisonValueAtIndex | TGetComparisonValueAtIndex<T> | A function that provides the value for comparison at a given index. |
length | number | The number of elements in the structure indexable to search. |
start | number | (Optional) The start index. |
Returns:
number
The index of the searched for item, else -1 if it cannot be found.
The indexable
parameter must be sorted ascending. Where there are multiple equal values the lowest index will be returned.
See arrayBinaryIndexOf().
// searching for the number 3 with start index 1 & length 2
const index = arrayBinaryIndexOf([1, 2, 3, 4], 3, (a, i) => a[i], 2, 1);
// index is 2