Hi,
I was wondering if anyone knows of any easy way to sort numbers from least to greatest in Visual Basic 6? For example, if you have 10 scores and want to display them in 1st place, second place, etc. How could you sort them?
Thanks
Pages 1
You are not logged in. Please login or register.
AudioGames.net Forum → Off-topic room → A VB Question
Hi,
I was wondering if anyone knows of any easy way to sort numbers from least to greatest in Visual Basic 6? For example, if you have 10 scores and want to display them in 1st place, second place, etc. How could you sort them?
Thanks
Hello,
There are thousands of sorting algorithms for numbers, for example, quicksort, bubble, and merge sort.
This simple code uses two loops, and checks if the previous item in an array is less than the current. If yes, no problems. If no, then it'll swap the two items.
The code:
dim a(1 to 10) as integer 'the aray for the ten numbers
'the two vars for the loop
dim i as byte
dim j as byte
'a temp variable for the swap
dim temp as long
For i = LBound(a) To UBound(a) - 1
For j = LBound(a) To UBound(a) - i
if a(j+1) < a(j) then 'compare the two neighbors
tmp = a(j) 'swap them
a(j) = a(j+1)
a(j+1) = tmp
end if
next j
next i
Code ends.
Hope this helps,
Robjoy
Hi robjoy,
Thanks for your help, but so far, I haven't got it to work. To test it I assigned each array in the variable a (like a(1), a(2), etc.) to some different out of order numbers, then i ran through the loop, and got a cyntax error on the line that said tmp = a(j) which is weird anyway because I thought the variable was called temp. I tried changing tmp to temp and got expected: end of statement. Am I doing something wrong?
hello,
you need to change tmp to temp, but otther than that, it should work.
I just didn't test it with vb6, that's why you had that stupid syntax error, lol.
AudioGames.net Forum → Off-topic room → A VB Question
Generated in 0.015 seconds (49% PHP - 51% DB) with 9 queries