BinarySearch

Description

The BinarySearch method performs a binary search on a sorted array to find the position where a value should be located. The array can be sorted in either ascending or descending order.

Syntax

public BinarySearch<T extends number | string>(
    array: T[],
    value: T,
    count: number = APIConstants.ARRAY_ENTIRE,
    start = 0,
    direction = APIConstants.SORT_ASCENDING
): number

Parameters

  • array: An array of numbers or strings to search in

  • value: The value to search for

  • count: Optional. The number of elements to search through. Default is ARRAY_ENTIRE

  • start: Optional. The starting index for the search. Default is 0

  • direction: Optional. The sort direction (SORT_ASCENDING or SORT_DESCENDING). Default is SORT_ASCENDING

Return Value

Returns a number representing the index where the value is or should be inserted to maintain the sort order.

Example

Last updated