Mega Code Archive

 
Categories / VB.Net Tutorial / Collections
 

Sort a custom object array

Public Class Tester     Public Shared Sub Main         Dim arrayToSort() As Employee = { _            New Employee("F", "O"), _            New Employee("V", "O"), _            New Employee("F", "A"), _            New Employee("V", "C"), _            New Employee("F", "G")}         Array.Sort(arrayToSort)         For Each food As Employee In arrayToSort             Console.WriteLine(food.ToString())         Next food     End Sub End Class Public Class Employee     Implements IComparable     Public FirstName As String     Public LastName As String     Public Sub New(ByVal theGroup As String, ByVal theItem As String)         FirstName = theGroup         LastName = theItem     End Sub     Public Overrides Function ToString() As String         Return FirstName & ": " & LastName     End Function     Public Function CompareTo(ByVal obj As Object) As Integer _             Implements System.IComparable.CompareTo         Dim compareValue As String         compareValue = obj.ToString()         Return String.Compare(Me.ToString(), compareValue)     End Function End Class F: A F: G F: O V: C V: O