There are a lot of ways that you could approach this.
The simplest is to just have a single array with the numbers. Then, keep an array representing query that you store the index of the max numbers in. You would just have to write a simple
for
statement to search through and find the index of the two largest numbers..
Something like this:
Dim newArray(4) As Integer
newArray(0) = 5
newArray(1) = 6
newArray(2) = 16
newArray(3) = 14
newArray(4) = 2
Dim q2(1) As Integer
q2(0) = -1
q2(1) = -1
For i = 0 To newArray.Length - 1
If q2(0) = -1 Then
q2(0) = i
ElseIf newArray(i) >= newArray(q2(0)) Then
q2(1) = q2(0)
q2(0) = i
ElseIf newArray(i) >= newArray(q2(1)) Then
q2(1) = i
End If
Next
At this point, with the values you gave,
q2(0) = 2
and
q2(1) = 3
or the indexes of the two highest numbers in the array. You can always choose to sort q2 too, if for instance
newArray(2) = 14
and
newArray(3) = 16
which would give
q2(0) = 3
and
q2(1) = 2
if you wanted to make sure that you were modifying the numbers based on their index order.
Then, to modify the numbers, you just modify
newArray(q2(0))
.
If you really wanted to do sorting and want to keep track of the original index, you will most likely have to use a multi-dimensional array.