Ali said:
I have a two arrays that I wish to sort. One is the index array (full
of floating point values). The other is a string array:
How would I do this in VB.NET?
You need to sort the float values, but whenever you change the order of that
array just make sure that you change the order of the string array at the
same time.
The following code should do what you want; it performs a simple
bubble-sort, sorting the floating point values into descending order and
keeping the associated descriptions synchronised.
\\\
'Create the arrays
Dim values() As Double = {-1.2, 12, 23.5, -100.2}
Dim descriptions() As String = {"textA", "textB", "textC", "textD"}
'Temporary values to help us swap array items
Dim tempValue As Double
Dim tempDescription As String
Dim i As Integer
Dim changeMade As Boolean
'Loop until we're finished sorting
Do
'Reset the changeMade flag
changeMade = False
'Loop through the items in the arrays except for the final item
For i = 0 To UBound(values) - 1
'Does the next item in the array have a larger value than
the current item?
If values(i + 1) > values(i) Then
'Yes, so we'll swap them over...
tempValue = values(i + 1)
values(i + 1) = values(i)
values(i) = tempValue
'Swap the descriptions too so that everything stays
synchronised
tempDescription = descriptions(i + 1)
descriptions(i + 1) = descriptions(i)
descriptions(i) = tempDescription
'Note that we've made a change so that we can try the
sort again.
changeMade = True
End If
Next
'Keep looping until nothing changes.
'Only at this point will we know that the arrays are fully
sorted
Loop Until changeMade = False
'Display the content of the arrays
For i = 0 To UBound(values)
Debug.WriteLine(values(i) & " : " & descriptions(i))
Next
///
Hope that helps,