Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting 2D Array

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
Hi all,

I have a 2D array which i want to sort in ascending order on the first column. The array is 20000 elements long, so it needs to be quite quick.

Can someone give an example piece of code for this task?

Thanks in advance

Mel (-:
 
Try this.
It works in my application.
---------------------------------------------------
Sub ProcSort(arr As Variant, numEls As Long, descending As Boolean)
Dim index As Long, index2 As Long, firstItem As Long
Dim distance As Long, value As Variant, Nval As Integer
If VarType(arr) < vbArray Then Exit Sub
firstItem = LBound(arr)
Do
distance = distance * 3 + 1
Loop Until distance > numEls
Do
distance = distance \ 3
For index = distance + firstItem To numEls + firstItem - 1
value = arr(index, 0)
Nval = arr(index, 1)
index2 = index
Do While (arr(index2 - distance, 0) > value) Xor descending
arr(index2, 0) = arr(index2 - distance, 0)
arr(index2, 1) = arr(index2 - distance, 1)
index2 = index2 - distance
If index2 - distance < firstItem Then Exit Do
Loop
arr(index2, 0) = value
arr(index2, 1) = Nval

Next
Loop Until distance = 1
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top