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:
  1. Public Class Form1
  2.  
  3.     Dim Websites As New List(Of URL)
  4.  
  5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  6.  
  7.         With Websites
  8.             .Add(New URL("VB Forums", "www.vbforums.com", True))
  9.             .Add(New URL("W3 Schools", "www.w3schools.com", False))
  10.             .Add(New URL("Google", "www.google.com", True))
  11.             .Add(New URL("Amazon", "www.amazon.com", False))
  12.         End With
  13.  
  14.         ' I need to sort the list 'Websites' by alphabetical order.
  15.         ' Using List.Sort crashes the program.
  16.         Websites.Sort()
  17.  
  18.         For Each url In Websites
  19.             ListBox1.Items.Add(url.ToString)
  20.         Next
  21.  
  22.     End Sub
  23.  
  24. End Class
  25.  
  26. ' Example class with 3 properties.
  27.  
  28. Public Class URL
  29.  
  30.     Property WebpageName As String
  31.     Property WebAddress As String
  32.     Property Favorite As Boolean
  33.  
  34.     Sub New(Name_ As String, Address_ As String, Favorite_ As Boolean)
  35.         WebpageName = Name_
  36.         WebAddress = Address_
  37.         Favorite = Favorite_
  38.     End Sub
  39.  
  40.     Public Overrides Function ToString() As String
  41.         Return WebpageName & " Url: " & WebAddress
  42.     End Function
  43.  
  44. End Class