ellebi General User

Joined: 11 Oct 2009 Posts: 13
|
Posted: Fri May 14, 2010 1:43 am Post subject: Sort an array of structures |
|
|
Can I sort an array of records defined with a "Type" construct? I try an example like this, but it does not work:
Type record
id As String
nam As String
End Type
Sub SortExample()
Dim Entry(3) As record
Dim i As Integer, i2 As Integer, temp As record
Dim bSomethingChanged As Boolean
' Fill the array
Entry(0).id = "030"
Entry(0).nam = "table"
Entry(1).id = "022"
Entry(1).nam = "chair"
Entry(2).id = "012"
Entry(2).nam = "couch"
Entry(3).id = "025"
Entry(3).nam = "mirror"
' Sort the array
For i = LBound(Entry()) To Ubound(Entry())
bSomethingChanged = False
For i2 = i + 1 To Ubound(Entry())
If StrComp(Entry(i).id, Entry(i2).id, 0) = 1 Then
temp = Entry(i)
Entry(i) = Entry(i2)
Entry(i2) = temp
bSomethingChanged = True
End If
Next i2
' If the array is already sorted then stop looping!
If Not bSomethingChanged Then Exit For
Next i
'display sort result
For i = LBound(Entry) To UBound(Entry)
msgbox Entry(i).id & chr(10) & Entry(i).nam
Next i
End Sub |
|