sortedIndex

array

Uses a binary search to determine the lowest index at which value should be inserted into array in order to maintain its sort order.

Signature

sortedIndex<T>(array: T[], value: T): number

Parameters

NameTypeDescription
arrayT[]The sorted array to inspect
valueTThe value to evaluate

Returns

number - Returns the index at which value should be inserted into array

Examples

Find insertion index

import { sortedIndex } from 'dashlite'

const result = sortedIndex([30, 50], 40)
console.log(result)

Output:

1

Available since version 1.0.0