Tuesday, October 18, 2011

Sorting and Searching in an ArrayList in VB.NET

In order to sort or search an ArrayList of objects you must first create a class that implements the IComparer interface.

Usually, this types of classes are nested within the object's class.

The nested class who implements the IComparer interface must implement the method Compare.

Let's assume we have a class called Person:
Public Class Person
    'Fields
    Private name As String
    Private age As Byte
    
    'Constructor
    Public Sub New(ByVal name As String, ByVal age as Byte)
        MyBase.New()
        Me.name = name
        Me.age = age
    End Sub
    
    'Properties
    Public ReadOnly Property Name As String
        Get
            Return Me.name
        End Get
    End Property
    
    Public ReadOnly Property Age As Byte
        Get
            Return Me.age
        End Get
    End Property
End Class
The syntax for creating an ArrayList of Person and populating it with a few entries would be:
Dim personList as new ArrayList()
personList.Add(new Person("John Doe", 42))
personList.Add(new Person("Mac McCormick", 25))
personList.Add(new Person("Jack Black", 33))
1.Sorting an ArrayList

In order to sort these entries according to the field name, you must write a class to implement the IComparer interface. To improve the code readability, you should nest the class in Person.
Public Class NameComparer
        Implements IComparer
        Public Function Compare(ByVal x As Object, ByVal y As Object) 
                                As Integer Implements IComparer.Compare
            Return Return New CaseInsensitiveComparer().Compare(
                               CType(x, Person).Name, CType(y, Person).Name)
        End Function
End Class
Since the Compare receives Object parameters you must use CType to change the object type to the type you send. This will come in handy later.

Now, if you want to sort the ArrayList, you could do it by:
personList.Sort(New Person.NameComparer())
If you want to sort the ArrayList according to age, you must create a new class to implement the IComparer logic for sorting according to age. This new class could be nested as well in Person.
Public Class AgeComparer
        Implements IComparer
        Public Function Compare(ByVal x As Object, ByVal y As Object) 
                                As Integer Implements IComparer.Compare
            Return New CaseInsensitiveComparer().Compare(
                         CType(x, Person).Age, CType(y, Person).Age)
            End If
        End Function
End Class 
The AgeComparer the same thing for the field Age (which is a Byte) that NameComparer does for the field Name (which is a String). The syntax for sorting according to age resembles the syntax for sorting according to name:
personList.Sort(New Person.AgeComparer())

2.Searching in an ArrayList

Searching for a value in an ArrayList is usually done by using the default Binary Search algorithm. By using this method you receive the index of the searched object. If the object is not found, you receive a negative number.
Dim index As Integer = personList.BinarySearch(new Person("John Doe", 42))
If (index >= 0) Then
     'Object was found
Else
     'Object was NOT found
End If
But what if we would want to search the ArrayList without using a Person object? Or if we only know the name of the person we would want to search for, but we wouldn't know its?

In order to do that, we need to define another implementation class of the IComparer. We cannot use the NameComparer interface since it treats the second object as a Person, but we would want it to treat it as a String.
Public Class StringNameComparer
        Implements IComparer
        Public Function Compare(ByVal x As Object, ByVal y As Object)
                                As Integer Implements IComparer.Compare
            Return New CaseInsensitiveComparer().Compare(
                         CType(x, Student).Name, CType(y, string))
        End Function
End Class
In order to search for a name you must:
Dim index As Integer = personList.BinarySearch("John Doe", New StringNameComparer)
If (index >= 0) Then
     'Object was found
Else
     'Object was NOT found
End If

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger