VB • Define an Employee Collection Class

Listing 5. Assume that you want to define an employee collection class that implements a custom sorting of the collection. You implement this kind of sort by defining a sorting class that implements the IComparer interface.


Public Class employeeCollection
   Inherits Generic.List(Of employee)
   Enum SortField      ID      Name    End Enum    Public Shadows Sub Sort(ByVal field _      As SortField)      Select Case field        Case SortField.ID          MyBase.Sort(New IDComparer)        Case SortField.Name          MyBase.Sort(New NameComparer)      End Select    End Sub    Private Class IDComparer      Implements Generic.IComparer(Of employee)
     Public Function Compare(ByVal x As employee, _        ByVal y As employee) As Integer _        Implements System.Collections.Generic. _        IComparer(Of employee).Compare        Return x.ID.CompareTo(y.ID)      End Function    End Class    Private Class NameComparer      Implements Generic.IComparer(Of employee)
     Public Function Compare(ByVal x As employee, _        ByVal y As employee) As Integer _        Implements System.Collections.Generic. _        IComparer(Of employee).Compare        Return x.Name.CompareTo(y.Name)      End Function    End Class End Class