BinaryFind (open)

Does a (case sensitive) binary search within a list

Syntax

LOADLIB "wh::util/algorithms.whlib";

INTEGER FUNCTION BinaryFind(VARIANT list, VARIANT element, FUNCTION PTR comparefunction)

Parameters

VARIANT list

Array to search in

VARIANT element

Element to search for

FUNCTION PTR comparefunction

Optional compare function, which must return whether the first parameter should be sorted before the second (signature: BOOLEAN FUNCTION func(VARIANT a, VARIANT b)

Return value

INTEGER

Position of the first element that is greater than the searched-for element, or the end of the list if no such element can be found.

Description

This function searches for an element within a sorted list.

Examples


STRING ARRAY list := [ "b", "d", "d", "f" ];

// Returns 3
INTEGER res := BinaryFind(list, "d");

// Returns -1
INTEGER res := BinaryFind(list, "a");