Mega Code Archive

 
Categories / VB.Net Tutorial / Class Module
 

Sort objects in an array

Imports System.Collections public class Test       public Shared Sub Main         Dim people(4) As Employee         people(0) = New Employee("R", "S")         people(1) = New Employee("S", "A")         people(2) = New Employee("T", "P")         people(3) = New Employee("H", "S")         people(4) = New Employee("E", "C")         ' Sort.         Array.Sort(people)         For i As Integer = 0 To people.GetUpperBound(0)             Console.WriteLine(people(i).ToString())         Next i    End Sub     End class Public Class Employee     Implements IComparable     Public FirstName As String     Public LastName As String     Public Sub New(ByVal first_name As String, ByVal last_name As String)         FirstName = first_name         LastName = last_name     End Sub     Public Overrides Function ToString() As String         Return LastName & ", " & FirstName     End Function     Public Function CompareTo(ByVal obj As Object) As Integer _      Implements System.IComparable.CompareTo         Dim other_Employee As Employee = DirectCast(obj, Employee)         Return String.Compare(Me.ToString, other_Employee.ToString)     End Function End Class A, S C, E P, T S, H S, R