2007-04-10 22:12:02 (edited by Mike 2007-04-10 22:12:21)

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

Regards,
Mike
Co-Founder, RS Games
www.rsgames.org

2007-04-12 16:38:25

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

----------
Robjoy, AKA Erion
Visit my site for all the things I do and to contact me.
You can also stop by for a slice of Pi

2007-04-12 21:59:55

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?

Regards,
Mike
Co-Founder, RS Games
www.rsgames.org

2007-04-13 06:04:29

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.

----------
Robjoy, AKA Erion
Visit my site for all the things I do and to contact me.
You can also stop by for a slice of Pi