Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default sorting an array of own type

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default sorting an array of own type

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

"Christian" wrote in message
ups.com...
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type Mismatch: array or user defined type expected ExcelMonkey Excel Programming 4 July 6th 06 03:40 PM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Excel Programming 6 November 9th 05 05:54 AM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Excel Programming 1 November 8th 05 04:21 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
What type of Array is this? davidm Excel Programming 2 June 29th 05 06:29 AM


All times are GMT +1. The time now is 02:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"