JumpyNET Posted May 24, 2008 Posted May 24, 2008 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? 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 Quote
Administrators PlausiblyDamp Posted May 24, 2008 Administrators Posted May 24, 2008 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... 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. 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. Dim x As New ShortCutFuncListClass x.SortByName() x.SortByActionNumb() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
JumpyNET Posted May 24, 2008 Author Posted May 24, 2008 Thank you that was exactly what I was looking for! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.