OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Sort an array of structures

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
ellebi
General User
General User


Joined: 11 Oct 2009
Posts: 13

PostPosted: Fri May 14, 2010 1:43 am    Post subject: Sort an array of structures Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group