Sort objects in an array : Object Instance « Class Module « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Class Module » Object Instance 
6.37.2.Sort objects in an array
Imports System.Collections
public class Test
  
   public Shared Sub Main
        Dim people(4As 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 = 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 ObjectAs 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
6.37.Object Instance
6.37.1.Create an object and call its method.
6.37.2.Sort objects in an array
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.