What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSort objects by different key fields by building a comparer class in Visual Basic .NET
DescriptionThis example shows how to sort objects by different key fields by building a comparer class in Visual Basic .NET.
Keywordscomparer, IComparer, sort, sortable, Array.Sort, VB.NET
CategoriesAlgorithms, Software Engineering, VB.NET
The CustomerInfoComparer class sorts CustomerInfo objects either by Name or ID. The CompareType variable indicates on which field the object sorts.

The Compare function returns -1, 0, or 1 to indicate whether one CustomerInfo object should be considered less than, equal to, or greater than another.

' Compares CustomerInfo objects for sorting.
Private Class CustomerInfoComparer
    Implements IComparer

    ' Determines whether we compare by name or ID.
    Public Enum CompareTypes
    End Enum
    Public CompareType As CompareTypes = CompareTypes.ById
    Public Sub New(ByVal compare_type As CompareTypes)
        CompareType = compare_type
    End Sub

    ' Compare two CustomerInfo objects.
    Public Function Compare(ByVal x As Object, ByVal y As _
        Object) As Integer Implements _
        Dim x_cust As CustomerInfo = DirectCast(x, _
        Dim y_cust As CustomerInfo = DirectCast(y, _

        Select Case CompareType
            Case CompareTypes.ById
                Return x_cust.ID.CompareTo(y_cust.ID)
            Case CompareTypes.ByName
                Return x_cust.Name.CompareTo(y_cust.Name)
        End Select
    End Function
End Class
The CustomerInfo class simply holds Name and ID data.
' Holds data.
Private Class CustomerInfo
    Public Name As String
    Public ID As Integer        ' We sort on this value.

    Public Sub New(ByVal new_name As String, ByVal new_id _
        As Integer)
        Name = new_name
        ID = new_id
    End Sub

    Public Overrides Function ToString() As String
        Return ID.ToString() & ". " & Name
    End Function
End Class
When the form loads, the program creates an array of CustomerInfo objects. It checks the radByName radio button to make the program display the obects sorted by Name.
Private m_Items(9) As CustomerInfo

Private Sub Form1_Load(...) Handles MyBase.Load
    ' Make some data.
    m_Items(0) = New CustomerInfo("Archer", 4)
    m_Items(1) = New CustomerInfo("Beck", 7)
    m_Items(2) = New CustomerInfo("Cantu", 2)
    m_Items(3) = New CustomerInfo("Deevers", 1)
    m_Items(4) = New CustomerInfo("Edwards", 9)
    m_Items(5) = New CustomerInfo("Finnagan", 3)
    m_Items(6) = New CustomerInfo("Guy", 5)
    m_Items(7) = New CustomerInfo("Hennesey", 8)
    m_Items(8) = New CustomerInfo("Irving", 6)
    m_Items(9) = New CustomerInfo("Jacquinth", 4)
    '' Uncomment to test duplicate objects.
    'm_Items(6) = New CustomerInfo("Finnagan", 3)
    'm_Items(7) = New CustomerInfo("Edwards", 9)
    'm_Items(8) = New CustomerInfo("Finnagan", 3)
    'm_Items(9) = New CustomerInfo("Deevers", 1)

    ' Start sorting by name.
    radByName.Checked = True
End Sub
When you click the By Name or By ID radio buttons, the program calls subroutine SortBy, passing it a parameter to tell it whether to sort by name or ID.

Subroutine SortBy makes a new CustomerInfoComparer object with the appropriate sort type. It calls Array.Sort to sort the array of items, passing in the CustomerInfoComparer so Array.Sort can use that object's Compare method to order the items. It then displays the items in their sorted order.

' Sort by name.
Private Sub radByName_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    If radByName.Checked Then _
End Sub

' Sort by ID.
Private Sub radById_CheckedChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    If radById.Checked Then _
End Sub

' Sort by name or ID.
Private Sub SortBy(ByVal compare_type As _
    ' Sort.
    Dim cust_comparer As New _
    Array.Sort(m_Items, cust_comparer)

    ' Display the items.
    For i As Integer = 0 To m_Items.Length - 1
    Next i
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.