sorting an array of own type

C

Christian

Hi,

A part of my program has the job to sort an array that consists of a
self-created data-type:

Type Test 'This is my own Data-Type
String1 As String
String2 As String
End Type


Public Sub SortTest()

Dim TestArray(5) As Test
TestArray(1).String1 = "Entry 1" 'Those values come from a
excel sheet in the real program, this is just
TestArray(1).String2 = "Entry A" 'for test reasons.
TestArray(2).String1 = "Entry 2"
TestArray(2).String2 = "Entry B"
(...)
TestArray(5).String1 = "Entry 5"
TestArray(5).String2 = "Entry E"

Dim Mark As Long, I As Long, EndIdx As Long, StartIdx As Long, _
Temp1 As String, Temp2 As String, Temp 5 As String

EndIdx = UBound(TestArray)
StartIdx = LBound(TestArray)
Do While EndIdx > StartIdx
Mark = StartIdx
For I = StartIdx To EndIdx - 1
If TestArray(I).String1 > TestArray(I + 1).String1 Then
Temp1 = TestArray(I).String1
TestArray(I).String1 = TestArray(I + 1).String1
TestArray(I + 1).String1 = Temp1
Temp2 = TestArray(I).String2
TestArray(I).String2 = TestArray(I + 1).String2
TestArray(I + 1).String2 = Temp2
(...)
Temp5 = TestArray(I).String5
TestArray(I).String5 = TestArray(I + 1).String5
TestArray(I + 1).String5 = Temp5

Mark = I
End If
Next I
EndIdx = Mark
Loop

End Sub


My problem is the following: the array doesn't consist of just 5
entries like here in the example, those are 15 in the real program and
can get more.
I'm searching for a way to avoid writing down the whole exchange-
mechanism in the IF...END IF procedure for 15 times. Is there a way to
change all 15 entries at once?

Code so far:

Temp1 = TestArray.String1 (1)
Temp2 = TestArray.String2 (1)
....
Temp15 = TestArray.String15 (1)

"Dream-code", anything like:

TempArray.AllSubEntries (1) = TestArray.AllSubEntries (1)

or do I have to write it seperately for all 15 Sub-entries?


Thanx for clues,

- Christian
 
N

NickHK

Christian,
Given your definition of the custom type, there is no such thing as
TestArray(I).String5

You are confusion the parts of the Type, with the elements of the array.

Also, unless you have set Option Base 1, you have 6 (0 to 5) elements here,
not 5 :
Dim TestArray(5) As Test

I would guess you want to sort by String1 first, then by String2 ???

One way would be to use a 2-D array intead of your Type, dump to worksheet,
sort as required, then pick it up again.

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top