C
Chris
I have a bubble sort for a 2-dimensional array that sorts a
string,number pair based on the number. The code for the sort is as
follows:
Private Sub SortArray(ByRef roundarray(,) As String)
Dim i, j, x As Integer
x = roundarray.GetUpperBound(0)
Dim tempname, tempnumber As String
For i = 0 To x - 1
For j = i + 1 To x
If CInt(roundarray(i, 1) < CInt(roundarray(i + 1, 1)))
Then
tempname = roundarray(i, 0)
tempnumber = roundarray(i, 1)
roundarray(i, 0) = roundarray(i + 1, 0)
roundarray(i, 1) = roundarray(i + 1, 1)
roundarray(i + 1, 0) = tempname
roundarray(i + 1, 1) = tempnumber
End If
Next j
Next i
End Sub
My problem is that when I call the sort the first time, it misses 1
sort so if the list it had to sort was:
Bob 10
Ed 3
Zeek 11
it returns
Bob 10
Zeek 11
Ed 3.
Regardless of the length of the list, it always puts the highest value
1 from the top instead of at the top. If I re-apply the sort, however,
it is put in the right order. I have a feeling it has something to do
with one of my loop limits, but I've tried increasing them by 1 without
any effect. I know the bubble sort is not terribly effective, but it
works for what I need to do right now.
Many thanks for any suggestions,
Chris
string,number pair based on the number. The code for the sort is as
follows:
Private Sub SortArray(ByRef roundarray(,) As String)
Dim i, j, x As Integer
x = roundarray.GetUpperBound(0)
Dim tempname, tempnumber As String
For i = 0 To x - 1
For j = i + 1 To x
If CInt(roundarray(i, 1) < CInt(roundarray(i + 1, 1)))
Then
tempname = roundarray(i, 0)
tempnumber = roundarray(i, 1)
roundarray(i, 0) = roundarray(i + 1, 0)
roundarray(i, 1) = roundarray(i + 1, 1)
roundarray(i + 1, 0) = tempname
roundarray(i + 1, 1) = tempnumber
End If
Next j
Next i
End Sub
My problem is that when I call the sort the first time, it misses 1
sort so if the list it had to sort was:
Bob 10
Ed 3
Zeek 11
it returns
Bob 10
Zeek 11
Ed 3.
Regardless of the length of the list, it always puts the highest value
1 from the top instead of at the top. If I re-apply the sort, however,
it is put in the right order. I have a feeling it has something to do
with one of my loop limits, but I've tried increasing them by 1 without
any effect. I know the bubble sort is not terribly effective, but it
works for what I need to do right now.
Many thanks for any suggestions,
Chris