|
-
Apr 10th, 2016, 09:52 PM
#1
Thread Starter
Addicted Member
Sort a List of Custom Classes
Hey everyone!
I need help sorting a list, containing a custom class. I want the list to be sorted alphabetically using the .ToString() function but it crashes on runtime. (My example code is below.)
Can somebody please teach me how to use Comparers?
Thanks a lot!
~Nic
vb Code:
Public Class Form1
Dim Websites As New List(Of URL)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With Websites
.Add(New URL("VB Forums", "www.vbforums.com", True))
.Add(New URL("W3 Schools", "www.w3schools.com", False))
.Add(New URL("Google", "www.google.com", True))
.Add(New URL("Amazon", "www.amazon.com", False))
End With
' I need to sort the list 'Websites' by alphabetical order.
' Using List.Sort crashes the program.
Websites.Sort()
For Each url In Websites
ListBox1.Items.Add(url.ToString)
Next
End Sub
End Class
' Example class with 3 properties.
Public Class URL
Property WebpageName As String
Property WebAddress As String
Property Favorite As Boolean
Sub New(Name_ As String, Address_ As String, Favorite_ As Boolean)
WebpageName = Name_
WebAddress = Address_
Favorite = Favorite_
End Sub
Public Overrides Function ToString() As String
Return WebpageName & " Url: " & WebAddress
End Function
End Class
-
Apr 10th, 2016, 10:08 PM
#2
Re: Sort a List of Custom Classes
To sort the list of class named Websites by WebpageName ascending try,,
Code:
Array.Sort(Websites, Function(a, b) a.WebpageName.CompareTo(b.WebpageName))
EDIT
I think thats ^ for arrays , try this instead,
Code:
Websites.Sort(Function(a, b) a.WebpageName.CompareTo(b.WebpageName))
-
Apr 10th, 2016, 10:13 PM
#3
Thread Starter
Addicted Member
Re: Sort a List of Custom Classes
I guess that works! However, I changed it to:
Code:
Websites.Sort(Function(a, b) a.ToString().CompareTo(b.ToString))
...so it was sorted by .ToString.
But if it is really that simple, then why does Microsoft have such a lengthy example and long process in https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx ?
-
Apr 10th, 2016, 10:29 PM
#4
Re: Sort a List of Custom Classes
I normally use an API so it sorts like Windows Explorer does, this way strings are sorted logically, so for example numbers in string are what you'd expect, 1, 2, 10, (and not 1, 10 ,2), like I have a list of tv shows and inside that list is a list of episodes for that show, when i want to sort the episodes by name I use a comparer I made...
Code:
' sort episodes by name
TvShows(index).Episodes.Sort(New LogicalEpisodeNameComparer)
' the comparer,
Public Class LogicalEpisodeNameComparer
Implements IComparer(Of EpisodeInfo)
Private Declare Unicode Function StrCmpLogicalW Lib "shlwapi" (ByVal s1 As String, ByVal s2 As String) As Integer
Private Function Compare(x As EpisodeInfo, y As EpisodeInfo) As Integer Implements System.Collections.Generic.IComparer(Of EpisodeInfo).Compare
Return StrCmpLogicalW(x.EpisodeName, y.EpisodeName)
End Function
End Class
' episode info class
Public Class EpisodeInfo
Public Property EpisodeName As String
Public Property EpisodeURL As String
Public Property Watched As Boolean
Public Property DateAdded As Date
Public Sub New(episodeName As String, episodeUrl As String, watched As Boolean, dateAdded As Date)
Me.EpisodeName = episodeName
Me.EpisodeURL = episodeUrl
Me.Watched = watched
Me.DateAdded = dateAdded
End Sub
End Class
Public Class TVshow
Public Property Name As String
Public Property Ratings As Single
Public Property Episodes As List(Of EpisodeInfo)
End Class
Last edited by Edgemeal; Apr 22nd, 2016 at 11:40 AM.
Reason: typos
-
Apr 10th, 2016, 10:29 PM
#5
Re: Sort a List of Custom Classes
 Originally Posted by NinjaNic
Firstly, it's demonstrating more than one way to sort and secondly there is always a certain amount of setup required in order to demonstrate a simple principle. The part from that lengthy example that corresponds to what you've been shown here is just this:
Code:
' This shows calling the Sort(Comparison(T) overload using
' an anonymous delegate method.
' This method treats null as the lesser of two values.
parts.Sort(Function(x As Part, y As Part)
If x.PartName Is Nothing AndAlso y.PartName Is Nothing Then
Return 0
ElseIf x.PartName Is Nothing Then
Return -1
ElseIf y.PartName Is Nothing Then
Return 1
Else
Return x.PartName.CompareTo(y.PartName)
End If
End Function)
That is just as simple as what you've been shown here except that it allows for either or both of the compared properties to not have a value. That code snippet alone would be useless without the setup of defining the PartClass and the PartName property though.
-
Apr 10th, 2016, 11:04 PM
#6
Thread Starter
Addicted Member
Re: Sort a List of Custom Classes
@Edgemeal: Interesting code. To me it looks like it's just a complicated way to compare the EpisodeName property but I'm probably wrong.
@jmc: Ok so that's more of a breakdown of the one-line..I'm guessing the -1, 1, and 0 is supposed to be moving the object up, down, or keep it where it is in the list?
Oh, and, thanks to you both for the help! I got what I needed and more, as usual.
-
Apr 11th, 2016, 12:36 AM
#7
Re: Sort a List of Custom Classes
 Originally Posted by NinjaNic
@jmc: Ok so that's more of a breakdown of the one-line..I'm guessing the -1, 1, and 0 is supposed to be moving the object up, down, or keep it where it is in the list?
Sorting a list of items involves repeatedly comparing pairs of items and rearranging them if appropriate. To that end, the Sort method requires a method that can be called repeatedly for pairs of items in the list and will provide an indication of whether the items need to be rearranged. If that method returns zero then it indicates that the items are considered equivalent for the purposes of ordering. A value less than zero indicates that the first item is considered the lesser and a value greater than zero indicates that the second value is the lesser. While any non-zero values will, it is convention to use -1 and 1 unless there is a more intuitive option.
If you call Sort with no parameters then it relies on the IComparable.CompareTo implementation of the items themselves. If you pass an object that implements IComparer then it relies on the Compare method of that object. If you pass a Comparison(Of T) delegate then the method it refers to must provide the same behaviour.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|