I've been struggling with this algorithm below. It is supposed to loas the times inputted from eight textboxes into an array (txtTD1, etc). It then sorts them, and assigns each a numeric rank, which is displayed to the user (in txtR1, etc).
Dim timeArr(0 To 7) As String timeArr(0) = txtTD1.Text timeArr(1) = txtTD2.Text timeArr(2) = txtTD3.Text timeArr(3) = txtTD4.Text timeArr(4) = txtTD5.Text timeArr(5) = txtTD6.Text timeArr(6) = txtTD7.Text timeArr(7) = txtTD8.Text System.Array.Sort(Of String)(timeArr) Dim count As Integer = "1" For i As Integer = 0 To 7 If timeArr(i) = txtTD1.Text Then txtR1.Text = count ElseIf timeArr(i) = txtTD2.Text Then txtR2.Text = count ElseIf timeArr(i) = txtTD3.Text Then txtR3.Text = count ElseIf timeArr(i) = txtTD4.Text Then txtR4.Text = count ElseIf timeArr(i) = txtTD5.Text Then txtR5.Text = count ElseIf timeArr(i) = txtTD6.Text Then txtR6.Text = count ElseIf timeArr(i) = txtTD7.Text Then txtR7.Text = count ElseIf timeArr(i) = txtTD8.Text Then txtR8.Text = count End If count = count + 1 Next
It works perfectly, except when there are duplicates in the array. Say, for example, both txtTD1.text and txtTD2.text have been inputted to have the same value. In that case, the following occurs:
txtTD1 | txtR1
10:10.10 | 2
30:30.30 | 4
20:20.20 | 3
So, it skips first place, assigns second place to the first instance of the repeated value, and then skips the second instance.
I've used message boxes as a means of testing whether the sorting algorithm is working correctly, and each time it tells me that it is (i.e., 10:10.10 is first, and the second 10:10.10 is second - would prefer them to both be first and the next to be third, but not exactly sure how to do this).
So there is clearly an issue with the 'matching' algorithm. I'm certain it has to do with the use of IF statements, but I'm not sure what other control structure to use to avoid this issue.
I'd be appreciative for any insights and assistance!