binary_search_insert_pos()

This function is used to find the correct insertion position for a value in a sorted array using binary search algorithm. It's a helper function used internally by the GM-I18n system. You can also use it on your own code if you need to find the insertion position for a value in a sorted array, not only exclusively for the GM-I18n system.

Syntax

Usage
binary_search_insert_pos(array, target);
Signature
function binary_search_insert_pos(
    array: any[],
    target: any
): number

Parameters

NameTypeDefaultDescription
arrayArrayThe sorted array to find the insertion position for the target value.
targetAnyThe target value to find the insertion position for in the array.

Returns

Integer (the insertion position for the target value in the array)

Examples

Create Event
// create a sorted array
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// find the insertion position for number 5 in the array
var index = binary_search_insert_pos(arr, 5);     // 4 (index of number 5 in the array)