Yeah, that won't work. It kind of looks like you are guessing at solutions rather than thinking them through. Write this down on paper:
Array Slot Value
0 11
1 9
2 13
3 17
4 6
5 8
Once you have that written down, you could add those values into your list (but don't bother, yet, the point is that you COULD add those values, so what you have on paper is what your list would look like if you DID add them in).
How many items are there? That number would be MyArray.Count, and you should see that there are 6 elements with indeces 0-5.
So, if you look at the line you underlined:
If MyArray.Count - 1 > MyArray.Count Then
What does this do? You have already determined that for the example, MyArray.Count = 6, so MyArray.Count -1 = 5. Since 6 is never less than 5, this If statement will ALWAYS be false. That's clearly not what you want to do. But what DO you want to do?
Next, take a look at that demo, and consider what each line of your method will do.
The first line is actually this (ignoring the two lines that clear things):
4??? Why set UpperSub to 4? Where did 4 come from? You mentioned earlier that you got this working for 5 items. Therefore the purpose for the 4 is suggested, since 4 would be the maximum index value in a 5 item list. If we look at the next line this is supported:
Code:
While SwapFlag = True And UpperSub >= 1
Clearly, you are looping a certain number of times, and since we later see that UpperSub is being decremented as the last item in the loop, you are counting down from 4 to 1. But your list no longer has four items, it has 6 items. So, would it work if you simply replaced UpperSub = 4 with UpperSub = 6 in the original code? Probably (though I haven't really looked at it), but that would leave you with the same initial problem that EG pointed out: You are hardcoding in the number of elements. What you want is to have the UpperSub, which is your number of iterations, be based on the number of elements in the list. What is that number of elements? It is MyArray.Count. However, you want UpperSub to be not the actual number of elements, but the maximum index, which is MyArray.Count - 1. So set UpperSub = MyArray.Count - 1 in your initial code.
Now you can step through each line in your loop doing the same thing on paper, and see whether or not you get the result you expect. Alternatively, you could set a breakpoint on the While statement, and step through each line of code examining the variables with each step to see whether they are what you expect them to be.