Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSort an array using custom sorting orders in VB .NET
DescriptionThis example shows how to Sort an array using custom sorting orders in VB .NET. It uses a class that implements IComparer for the class being sorted.
Keywordssort, compare, comparer, IComparer, sort order, order
CategoriesAlgorithms, VB.NET
 
This example sorts Employee objects either by last name followed by first name, or by first name followed by last name.

The EmployeeComparer class compares two Employee objects. Its constructor initializes the m_FirstLast variable that determines whether the object compares two Employees in first name/last name order.

The class implements the IComparer interface. This interface defines the Compare method, which compares two objects and returns -1, 0, or 1 depending on whether the first object should be considered before, equal to, or after the second object. Depending on the m_FirstLast variable, the class makes strings representing the two objects and compares them.

 
Public Class EmployeeComparer
    Implements IComparer

    Private m_FirstLast As Boolean

    Public Sub New(ByVal first_last As Boolean)
        m_FirstLast = first_last
    End Sub

    ' Compare the two employees.
    Public Function Compare(ByVal x As Object, ByVal y As _
        Object) As Integer Implements _
        System.Collections.IComparer.Compare
        Dim empx As Employee = DirectCast(x, Employee)
        Dim empy As Employee = DirectCast(y, Employee)
        Dim txtx As String
        Dim txty As String

        If m_FirstLast Then
            txtx = empx.FirstName & "," & empx.LastName
            txty = empy.FirstName & "," & empy.LastName
        Else
            txtx = empx.LastName & "," & empx.FirstName
            txty = empy.LastName & "," & empy.FirstName
        End If

        Return String.Compare(txtx, txty)
    End Function
End Class
 
The program initializes an array of Employee objects named m_Employees. When the user clicks the radFirstLast RadioButton, the program makes a new EmployeeComparer object initialized to sort by first name followed by last name. It then calls the Array.Sort method, passing it the array and the comparer. The program then displays the result.

When the user clicks the radLastFirst RadioButton, the program sorts by last name followed by first name in a similar way.

 
Private m_Employees() As Employee = { _
    New Employee("Bill", "Able"), _
    New Employee("Sam", "Mars"), _
    New Employee("Ann", "Fisher"), _
    New Employee("Cindy", "Deevers"), _
    New Employee("Xerxes", "Conrad"), _
    New Employee("Phillip", "Fry") _
}

Private Sub radFirstLast_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles radFirstLast.Click
    ' Make the proper comparer.
    Dim employee_comparer As New EmployeeComparer(True)

    ' Sort the array.
    Array.Sort(m_Employees, employee_comparer)

    ' Display the results.
    DisplayEmployees()
End Sub

Private Sub radLastFirst_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles radLastFirst.Click
    ' Make the proper comparer.
    Dim employee_comparer As New EmployeeComparer(False)

    ' Sort the array.
    Array.Sort(m_Employees, employee_comparer)

    ' Display the results.
    DisplayEmployees()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated