andy7789
asked on
VBA Excel: sorting arrays
Hi X-perts,
I have two arrays:
names(1 to 20) ---- a string array
values(1 to 20) ----- a numerical array (double)
Both arrays are linked, i.e. i-th element of names corresponds to i-th element of values.
I need to sort both arrays on values, i.e. after sorting the 2nd array (values) will have all decrementing values and its i-th element will correspond to the same name as before sorting (i-th element of names)
Please suggest the most elegant way of doing that.
Thanks
I have two arrays:
names(1 to 20) ---- a string array
values(1 to 20) ----- a numerical array (double)
Both arrays are linked, i.e. i-th element of names corresponds to i-th element of values.
I need to sort both arrays on values, i.e. after sorting the 2nd array (values) will have all decrementing values and its i-th element will correspond to the same name as before sorting (i-th element of names)
Please suggest the most elegant way of doing that.
Thanks
ASKER
I need a VBA solution. Something like the attached code, but with a better performance
Public Function SortArray(ByRef TheArray As Variant)
Sorted = False
Do While Not Sorted
Sorted = True
For X = 0 To UBound(TheArray) - 1
If TheArray(X) > TheArray(X + 1) Then
Temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = Temp
Sorted = False
End If
Next X
Loop
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Select all 40 cells.
From the menu/ribbon, choose Sort, or use the keyboard - Alt-D, Alt-S
Options: no header, column (choose the column containing the values)