PDA

View Full Version : Solved: Function to sort multi dimension array - help



Movian
03-16-2009, 07:44 AM
Hi,
i have a sub that goes through a word document and creates a multi dimension array of the following format (0 to 1, 0 to Variable) as string

where by (0,0) would equal a table name and (1,0) would equal a field name. The reason this might seem backwards is because for some reason access will only allow me to redim preserve the second array block (0, this one)

so any way, i have been looking around at all the different options. The array shouldn't be any bigger than say 100 array values (on the second column, meaning 200 total) so a bubble sort would be usable. What i am trying to find is a function (or create a function) that when passed an array, will return an array that is sorted on the identified column. Alternatively it can be a sub that sorts the Actual array parsed to it. However i am aware that you cant make an array a public variable. so i am unsure as to how to go about it. Also if the function returned a NEW array would i be able to do somthing akin to

NewArray = MovianSort(oldarray,2)
?
or is that just wishfull thinking ?

CreganTur
03-16-2009, 08:52 AM
Maybe I'm missing the point of your post... but you can pass an array as a parameter to another sub/function. You just have to declare the parameter as a Variant. You can also declare your Function as a variant.

Movian
03-16-2009, 09:16 AM
ok so i CAN do for example the following.

dim array1() as string
dim array2() as variant
dim counter as integer

for counter = 0 to 10
array1(counter) = counter
next
array2() = sort array1()

for counter = 0 to 10
msgbox array2(counter)
next


if thats the case great ! :D :)

I also found some example code for a muli dimensional array sort on a single column.

Public Sub QuickSort(ByRef SortArray As Variant, ByVal First As Long, ByVal Last As Long, ByVal PrimeSort As Integer, ByVal Ascending As Boolean)
Dim Low As Long, High As Long, i As Integer
Dim Temp As Variant, List_Separator As Variant
Dim TempArray() As Variant
ReDim TempArray(UBound(SortArray, 1))
Low = First
High = Last
List_Separator = SortArray(PrimeSort, (First + Last) / 2)
Do
If Ascending = True Then
Do While (SortArray(PrimeSort, Low) < List_Separator)
Low = Low + 1
Loop
Do While (SortArray(PrimeSort, High) > List_Separator)
High = High - 1
Loop
Else
Do While (SortArray(PrimeSort, Low) > List_Separator)
Low = Low + 1
Loop
Do While (SortArray(PrimeSort, High) < List_Separator)
High = High - 1
Loop
End If
If (Low <= High) Then
For i = LBound(SortArray, 1) To UBound(SortArray, 1)
TempArray(i) = SortArray(i, Low)
Next
For i = LBound(SortArray, 1) To UBound(SortArray, 1)
SortArray(i, Low) = SortArray(i, High)
Next
For i = LBound(SortArray, 1) To UBound(SortArray, 1)
SortArray(i, High) = TempArray(i)
Next
Low = Low + 1
High = High - 1
End If
Loop While (Low <= High)
If (First < High) Then QuickSort SortArray, First, High, PrimeSort, Ascending
If (Low < Last) Then QuickSort SortArray, Low, Last, PrimeSort, Ascending
End Sub


i will try and modify this and turn it into a function so you can pass it an array and it will return the sorted array. I personally think that would be a very usefull function.

Also anoyingly i discovered after all this research that i don't need to sort the array as Word Automaticly aranges its bookmarks in alphabeticle order. Therefore when i go through the document and insert each bookmark name and range text into an array it has allready been sorted into the alphabetical order by word. (the reason i wanted to do this was so that i could open a table once and send a block of fields, then close the table and move to the next. Instead of opening and closing tables for EACH bookmark which will take additional processing time.)

so all in all i think this answers the main question. If someone wants a crack at converting this into a function that returns the array before i get a chance then please feel free. But i will post my solution when i get a chance to finish it.