Sort Array?
-
Hi, I'm having a little problem sorting a 2 dimensional array, and I was wondering if someone could help. I want to sort the information in my array in decending numerical order. An example of my information is as follows: - [50] [Bananas] [10] [Lemons] [30] [Oranges] [100] [Apples] I'd like it to sort the information from highest to lowest. I'm using VB6, is this possible? Any help would be much appreciated. Cheers Ben
-
Hi, I'm having a little problem sorting a 2 dimensional array, and I was wondering if someone could help. I want to sort the information in my array in decending numerical order. An example of my information is as follows: - [50] [Bananas] [10] [Lemons] [30] [Oranges] [100] [Apples] I'd like it to sort the information from highest to lowest. I'm using VB6, is this possible? Any help would be much appreciated. Cheers Ben
I'm afraid that there's no quick to way to do this, you'd have to do some coding. The best way, of course, if to implement a bubble sort through the first dimension, and change the second dimension accordingly Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark Twain -
Hi, I'm having a little problem sorting a 2 dimensional array, and I was wondering if someone could help. I want to sort the information in my array in decending numerical order. An example of my information is as follows: - [50] [Bananas] [10] [Lemons] [30] [Oranges] [100] [Apples] I'd like it to sort the information from highest to lowest. I'm using VB6, is this possible? Any help would be much appreciated. Cheers Ben
I always sort arrays using
For
loops. It might be a little slow for large arrays, but it always works for me:Dim myNewArray() As String, q As Integer, r As Integer, s As Integer, t As Integer
ReDim myNewArray(UBound(myOldArray, 0), UBound(myOldArray, 1))
r = 9999999 'Just a big number (bigger than the biggest number in your array)
s = 0
For q = 0 To UBound(myOldArray, 0)
If myOldArray(q, 0) <= r Then
For t = 0 To UBound(myOldArray, 1)
myNewArray(s, t) = myOldArray(q, t)
Next
s = s + 1
r = myOldArray(q, 0)
End If
NextI haven't tested this code, since I don't have VB6 installed at the place I currently am, but it should work. Just replace
myOldArray
by your array. You could even put this in a function, returning aString()
. "Make peace, not war"