Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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()

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...