DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

How to sort multiple dimension array?

By: rekha singh | 18 Jan 2010 5:01 pm

How to sort multiple dimension array?
 
Ex.
Structure Data
Dim IDno as String
Dim Fname as String
Dim Lname as String
Dim Age as Integer
End Structure
 
Dim Info(10) as Data
 
I want to sort the array by using IDno or Fname.
Any idea? Thanks in advance...


 

Comments

Why not use an arrayList? I put together a quick example below.
Example:

Imports System
Imports System.Collections
Imports Microsoft.VisualBas ic

Public Class Person
Dim myIDno as String
Dim myFname as String
Dim myLname as String
Dim myAge as Integer

' Ease of Use Constructor
Public Sub New(ByVal pIDno As String, _
ByVal pFname As String, _
ByVal PLname As String, _
ByVal pAge as Integer)
myIDno = pIDno
myFname = pFname
myLname = pLname
myAge = pAge
End Sub ' New

Property IDno() as String
Get
Return myIDno
End Get
Set(ByVal Value as String)
myIDno = Value
End Set
End Property ' IDno

Property Fname() as String
Get
Return myFname
End Get
Set(ByVal Value as String)
myFname = Value
End Set
End Property ' Fname

Property Lname() as String
Get
Return myLname
End Get
Set(ByVal Value as String)
myLname = Value
End Set
End Property ' Lname

Property Age() as Integer
Get
Return myAge
End Get
Set(ByVal Value as Integer)
myAge = Value
End Set
End Property ' Age
End Class ' Person

Public Class SortByIDNo
Implements IComparer
' Case Insensitive compare on IDNo
Public Function Compare(ByVal info1 As Object, _
ByVal infor2 As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComp arer().Compare _
(info1.IDno, info2.IDno)
End Function ' IComparer.Compare
End Class ' SortByIDno

Public Class SortByFname
Implements IComparer
' Case Insensitive compare on Fname
Public Function Compare(ByVal info1 As Object, _
ByVal infor2 As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComp arer().Compare _
(info1.Fname, info2.Fname)
End Function ' IComparer.Compare
End Class ' SortByFname

Public Class SampleArrayListSort ing

Public Shared Main()
Dim Info As New ArrayList()
Info.Add(New Person('ID202' ,'John',' Smith',34) )
Info.Add(New Person('ID107' ,'Mary',' Poppins', 27))
Info.Add(New Person('ID502' ,'Xavier' ,'DuBlanc' ,45))

' Displays the values of the ArrayList.
Console.WriteLine( _
"The ArrayList initially contains the following values:")
PrintIndexAndValues (Info)

' Sorts the values of the ArrayList using IDno.
Dim byID = New SortByIDno()
Info.Sort(byID)
Console.WriteLine( "After sorting by ID:")
PrintIndexAndValues (Info)

' Sorts the values of the ArrayList using Fname.
Dim byFname= New SortByFname( )
Info.Sort(byFname)
Console.WriteLine( "After sorting by Fname:")
PrintIndexAndValues (Info)

End Sub 'Main

Public Shared Sub PrintIndexAndValues (myList As IEnumerable)
Dim i As Integer = 0
Dim obj As Person
For Each obj In myList
Console.WriteLine( vbTab + "[{0}]:" + vbTab + _
"{1}" + vbTab + _
"{2}" + vbTab + _
"{3}" + vbTab + _
"{4}" + vbTab, _
i, obj.IDno(),obj. Fname(),obj. Lname(),obj. Age())
i = i + 1
Next obj
Console.WriteLine( )
End Sub 'PrintIndexAndValue s

End Class 'SamplesArrayListSo rting

 

By: rekha singh | 18 Jan 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move