Export (0) Print
Expand All

ArrayList.BinarySearch Method (Object, IComparer)

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

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Overridable Function BinarySearch ( _
	value As Object, _
	comparer As IComparer _
) As Integer

Parameters

value
Type: System.Object

The Object to locate. The value can be Nothing.

comparer
Type: System.Collections.IComparer

The IComparer implementation to use when comparing elements.

-or-

Nothing to use the default comparer that is the IComparable implementation of each element.

Return Value

Type: System.Int32
The zero-based index of value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than value or, if there is no larger element, the bitwise complement of Count.

ExceptionCondition
ArgumentException

comparer is Nothing and neither value nor the elements of ArrayList implement the IComparable interface.

InvalidOperationException

comparer is Nothing and value is not of the same type as the elements of the ArrayList.

The comparer customizes how the elements are compared. For example, you can use a CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.

If comparer is provided, the elements of the ArrayList are compared to the specified value using the specified IComparer implementation. The elements of the ArrayList must already be sorted in increasing value according to the sort order defined by comparer; otherwise, the result might be incorrect.

If comparer is Nothing, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. The elements of the ArrayList must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.

Comparing Nothing with any type is allowed and does not generate an exception when using IComparable. When sorting, Nothing is considered to be less than any other object.

If the ArrayList contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.

If the ArrayList does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the ArrayList, this index should be used as the insertion point to maintain the sort order.

This method is an O(log n) operation, where n is Count.

The following example creates an ArrayList of colored animals. The provided IComparer performs the string comparison for the binary search. The results of both an iterative search and a binary search are displayed.

Imports System
Imports System.Collections

Public Class SimpleStringComparer
    Implements IComparer

    Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
          Dim cmpstr As String = CType(x, String)
          Return cmpstr.CompareTo(CType(y, String))
    End Function 
End Class 

Public Class MyArrayList
    Inherits ArrayList

    Public Shared Sub Main()
        ' Creates and initializes a new ArrayList. 
        Dim coloredAnimals As New MyArrayList()

        coloredAnimals.Add("White Tiger")
        coloredAnimals.Add("Pink Bunny")
        coloredAnimals.Add("Red Dragon")
        coloredAnimals.Add("Green Frog")
        coloredAnimals.Add("Blue Whale")
        coloredAnimals.Add("Black Cat")
        coloredAnimals.Add("Yellow Lion")

        ' BinarySearch requires a sorted ArrayList.
        coloredAnimals.Sort()

        ' Compare results of an iterative search with a binary search 
        Dim index As Integer = coloredAnimals.IterativeSearch("White Tiger")
        Console.WriteLine("Iterative search, item found at index: {0}", index)

        index = coloredAnimals.BinarySearch("White Tiger", New SimpleStringComparer())
        Console.WriteLine("Binary search, item found at index:    {0}", index)
    End Sub 

    Public Function IterativeSearch(finditem As Object) As Integer 
        Dim index As Integer = -1

        For i As Integer = 0 To MyClass.Count - 1
            If finditem.Equals(MyClass.Item(i))
                index = i
                Exit For 
            End If 
        Next i
        Return index
    End Function 
End Class 

' This code produces the following output. 

' Iterative search, item found at index: 5 
' Binary search, item found at index:    5 
'

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft