findNearestSortedIndex

Finds the sorted position nearest to a target within a prefix

Available since @roku-sdk/flex-grid 3.3.0

import { findNearestSortedIndex } from "@roku-sdk/flex-grid/geometry";

Also exported as findNearestSortedIndex from @roku-sdk/flex-grid (deprecated).

Deprecated: findNearestSortedIndex. Import from @roku-sdk/flex-grid/geometry instead. The root export will be removed in the next major version.

findNearestSortedIndex(positions: number[], count: number, target: number): number

Description

Finds the sorted position nearest to a target within a prefix.

Positions must be non-decreasing in the searched prefix. The count is capped at the array length. An empty prefix or a count of one returns zero. Targets outside the prefix select its nearest endpoint; equal-distance ties select the lower index.

Parameters

NameTypeDescription
positionsnumber[]Sorted positions in pixels.
countnumberNumber of positions to search, as a non-negative integer.
targetnumberTarget position in pixels.

Return values

The zero-based nearest index, or zero for an empty prefix.

Example

findNearestSortedIndex([0, 100, 200], 3, 160); // 2

What’s Next

Did this page help you?