ArrayList: BinarySearchEx

BinarySearchEx

Searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.



 Public Function BinarySearchEx(
	  ByVal Index As Long,
	  ByVal Count As Long,
	  ByRef Value As Variant,
	  Optional ByVal Comparer As IComparer ) As Long

Parameters

Index
[ByVal] Long. The zero-based index to begin the search.
Count
[ByVal] Long. The number of elements to search in from the Index.
Value
[ByRef] Variant. The value to search for. The value can be Null or Empty.
Comparer
[ByVal] Optional. IComparer. A comparer object to use when comparing values.
-or-
Nothing to use the default comparer.

Return Values

Long -  The index at which the value was found, or a negative value indicating the value was not found.

Remarks

The search assumes the ArrayList is sorted.

The default comparison method requires the values to be of the same intrinsic Visual Basic datatype. A vbLong will fail to compare against a vbInteger, for instance. Objects must implement the IComparable interface or an exception will be thrown.

The comparison behaviour can be overridden by supplying a custom IComparer compatible object.

If the return value is negative, then the value was not found in the list. To determine where the value should have been found, negate (Not) the return value.

Exceptions

Exception Condition
ArgumentException Index and Count do not denote a valid range in the ArrayList.
-or-
Comparer is Nothing and neither Value nor the elements of ArrayList implement the IComparer interface.
InvalidOperationExceptionComparer is Nothing and Value is not of the same type as the elements of the ArrayList.
ArgumentOutOfRangeException Index is less than zero.
-or-
Count is less than zero.

See Also

Project CorLib Overview

Class ArrayList Overview

IComparer

IComparable