
04-07-2014, 01:40 AM
|
Competent Performer
|
|
Join Date: Jan 2014
Location: Germany
Posts: 136
|
|
Sorting algorithm for two dimensional array
Hi to all of you!
My problem today: some sorting algorithm for a two dimensional array. The array consists of (position in text, name of respective bookmark). I want to sort the bookmarks according to their position in the text (first field of array).
If possible I use you the "WordBasic.SortArray" to quick sort arrays. But WordBasic.SortArray fails for sorting numbers.
So I googled, and googled, and googled … and found a sorting algorithm which works fine for short single dimensional arrays.
Code:
Sub BubbleSort(list())
Dim First As Integer, Last As Long
Dim i As Long, j As Long
Dim Temp As Long
First = LBound(list)
Last = UBound(list)
For i = First To Last - 1
For j = i + 1 To Last
If list(i) > list(j) Then
Temp = list(j)
list(j) = list(i)
list(i) = Temp
End If
Next j
Next i
End Sub
But I did not succeed in adjusting it to sorting a two dimensional array.
The other solution is here: http://www.cpearson.com/Excel/SortingArrays.aspx, but this code is (seems to be) a bit OTT for my purpose.
I considered writing the array to a table, sort, and back to array, but that’s not my favored solution for Word. As a workaround I use two arrays: Array1=the positions, Array2=position and name. After sorting Array1 with BubbleSort, I 'join' the two arrays and the result is a bookmark array sorted by position. Purpose: use shortcuts to toggle between any number of next - previous bookmarks.
In the end it works, but sorting a two dimensional array still remains an issue and I would appreciate any help.
Thanks
NP
Last edited by NobodysPerfect; 04-07-2014 at 04:01 AM.
|