Making multiple compare methods

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
I have a class with a list of string and integer pairs. How could I make two methods for sorting the ShortFuncs list - one by the string and one by the integer?

Code:
    Public Class ShortCutFuncListClass
        Private Class ShortFuncClass
            Public Name As String
            Public ActionNumb As Integer
            Public Sub New(ByVal par_Name As String, ByVal par_ActionNumb As Integer)
                Name = par_Name
                ActionNumb = par_ActionNumb
            End Sub
        End Class
        Private ShortFuncs As List(Of ShortFuncClass)
        Public Sub New()
            ShortFuncs = New List(Of ShortFuncClass)
            ShortFuncs.Add(New ShortFuncClass("Playback: Play/Pause", 0))
            ShortFuncs.Add(New ShortFuncClass("Playback: Next", 1))
            ShortFuncs.Add(New ShortFuncClass("Playback: Previous", 2))
            ShortFuncs.Add(New ShortFuncClass("Playback: Exit", 3))
        End Sub

        Public Sub SortByName()
            '?????????????????
        End Sub
        Public Sub SortByActionNumb()
            '?????????????????
        End Sub
    End Class
 
Easiest way is to provide the ShortFunc class with a couple of functions that can do the actual comparison required by the sort routine.

http://msdn.microsoft.com/en-us/library/tfakywbh.aspx gives the details.

As a more solid example I've extended your ShortFuncClass...
Visual Basic:
Private Class ShortFuncClass
        Public Name As String
        Public ActionNumb As Integer
        Public Sub New(ByVal par_Name As String, ByVal par_ActionNumb As Integer)
            Name = par_Name
            ActionNumb = par_ActionNumb
        End Sub

        Public Shared Function CompareName(ByVal a As ShortFuncClass, ByVal b As ShortFuncClass) As Integer
            Return a.Name.CompareTo(b.Name)
        End Function

        Public Shared Function CompareNumber(ByVal a As ShortFuncClass, ByVal b As ShortFuncClass) As Integer
            Return a.ActionNumb.CompareTo(b.ActionNumb)
        End Function

    End Class

and the two sort methods simply pass the relevant delegate into the List.Sort method using one of it's overloads.
Visual Basic:
Public Class ShortCutFuncListClass
 'nested class snipped - posted above

    Private ShortFuncs As List(Of ShortFuncClass)
    Public Sub New()
        ShortFuncs = New List(Of ShortFuncClass)
        ShortFuncs.Add(New ShortFuncClass("Playback: Play/Pause", 0))
        ShortFuncs.Add(New ShortFuncClass("Playback: Next", 1))
        ShortFuncs.Add(New ShortFuncClass("Playback: Previous", 2))
        ShortFuncs.Add(New ShortFuncClass("Playback: Exit", 3))
    End Sub
    Public Sub SortByName()
        ShortFuncs.Sort(AddressOf ShortFuncClass.CompareName)
    End Sub
    Public Sub SortByActionNumb()
        ShortFuncs.Sort(AddressOf ShortFuncClass.CompareNumber)
    End Sub

End Class

These two functions do the required sorts - try the following snippet and notice the sort order changes if you step through.
Visual Basic:
Dim x As New ShortCutFuncListClass
x.SortByName()
x.SortByActionNumb()
 
Back
Top