ArrayList.BinarySearch (Object, IComparer)
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()
Dim coloredAnimals As New MyArrayList()
coloredAnimals.Add("A")
coloredAnimals.Add("B")
coloredAnimals.Add("C")
coloredAnimals.Add("D")
coloredAnimals.Add("E")
coloredAnimals.Add("F")
coloredAnimals.Add("G")
coloredAnimals.Sort()
Dim index As Integer = coloredAnimals.IterativeSearch("A")
Console.WriteLine("Iterative search, item found at index: {0}", index)
index = coloredAnimals.BinarySearch("A", 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
'
Related examples in the same category