# Solved: visual basic problems sorting numbers



## Sysco (Apr 13, 2005)

hi,
I'm trying to sort 6 numbers in descending order from best to worst and store them into new variables using VB. I can't seem to be able to do it and need any help you can give me as soon as possible.

I have attached the code (in zip) and I have all the comments in there relating to what I want to do.


----------



## OBP (Mar 8, 2005)

Can you post the VB on here please?


----------



## Sysco (Apr 13, 2005)

of course, here it is:

Option Explicit
Dim one, two, three, four, five, six As Integer
Dim best, secondbest, thirdbest, fourthbest, fifthbest, sixthbest As Integer

'dont worry about any checks, they're not included here 

Private Sub Command1_Click()

'NOW, WHAT I NEED HERE IS THIS:
'1. STORE 6 SCORE SUMS INTO 6 VARIABLES
' (example of this formula is below);

one = Val(lblinputA.Caption) + Val(lblinputA_bonus.Caption)
two = Val(lblinputB.Caption) + Val(lblinputB_bonus.Caption)
three = Val(lblinputC.Caption) + Val(lblinputC_bonus.Caption)
four = Val(lblinputD.Caption) + Val(lblinputD_bonus.Caption)
five = Val(lblinputE.Caption) + Val(lblinputE_bonus.Caption)
six = Val(lblinputF.Caption) + Val(lblinputF_bonus.Caption)

'2. ARRANGE THOSE 6 VARIABLES IN ORDER AND THEN PLACE THOSE IN NEW VARIABLES
' SO IT IS EASIER TO WORK WITH, FOR EXAMPLE, PLACE THE BEST SCORE INTO A NEW
' VARIABLE "BEST", THEN "SECONDBEST" AND SO ON.
' (i dont know how to do this...

'3. TAKE 50% FROM THE BEST AND 20% FROM THE LAST TWO, OR THE TWO WORST SCORES
' AND ADD THE REST TO MAKE UP A TOTAL.
'THIS LAST FORMULA SHOULD GO LIKE THIS:

lblTOTAL.Caption = (best * 50 / 100) + secondbest + thirdbest + fourthbest + (fifthbest * 20 / 100) + (sixthbest * 20 / 100)

'what I would also like is that "best" variable be associated with the label which it belongs or
'more precisely to the player that this score has been assigned, this is not necessary but would
'be good... also, rather than doing this, would it be possible to arrange all the labels containing the names
', scores and bonuses in descending order from best to worst from top to bottom?? that way having it neater?

End Sub


----------



## OBP (Mar 8, 2005)

Is this going to be limited to just 6 values or do you need to have it flexible?


----------



## Kelly_in_LA (Jun 16, 2004)

I think you will be very pleased with what you can do if you spend some time reading about basic sorting algorithms. If you search for "visual basic" "bubble sort" "insertion sort", you can find many subroutines (macros) already written for you. However, you may find it easier to just read a plain English description about what the process is that a bubble sort (or any other sort) actually does, step by step. Once you understand the process, the code you need to write is fairly quick and simple.

Try these links:

Insertion Sort

Insertion Sort - Visual Basic Code

Selection Sort

Sorry I don't have time to go into more detail -- I'm rushing through here on my lunch break.

Good luck!! I'll check this thread on the weekend (I hope) to see if I can help further.

-Kelly


----------



## OBP (Mar 8, 2005)

Sysco, this is a modified version of the bubble sort that kelly mentions, run this and you will see that it sorts the values in to descending order and carries the letter which represents your lblinputA.Caption
To use this you use variable arrays rather just variables as you proposed.

Private Sub CommandButton1_Click()
Dim value1(6) As Double, value2(6) As String, x As Integer, value3(6) As Integer

value1(1) = 6: value2(1) = "a"
value1(2) = 2: value2(2) = "b"
value1(3) = 1: value2(3) = "c"
value1(4) = 5: value2(4) = "d"
value1(5) = 4: value2(5) = "e"
value1(6) = 3: value2(6) = "f"

Dim counter1 As Long
Dim counter2 As Long
Dim Temp As Long, temp2 As String

'Which bubbling pass
 For counter1 = 1 To 6
'Which comparison
For counter2 = 1 To 6 - counter - 1

'Compare this item to the next item
If value1(counter2) < value1(counter2 + 1) Then
'Swap
Temp = value1(counter2)
temp2 = value2(counter2)
value1(counter2) = value1(counter2 + 1)
value2(counter2) = value2(counter2 + 1)
value1(counter2 + 1) = Temp
value2(counter2 + 1) = temp2
End If

Next counter2
Next counter1
For x = 1 To 6
MsgBox value1(x) & " " & value2(x)
Next x
End Sub


----------



## Sysco (Apr 13, 2005)

Thanks Kelly, I'll read through those as soon as I get time.

Now, OBP, that last code worked perfect except the Counter wasn't defined as integer so I did that and also changed this bit here:

value1(1) = one: value2(1) = "a"
value1(2) = two: value2(2) = "b"
value1(3) = three: value2(3) = "c"
value1(4) = four: value2(4) = "d"
value1(5) = five: value2(5) = "e"
value1(6) = six: value2(6) = "f"

BUT; there is just one more thing I'd like to do and that's to have the values stored into the 6 new variables, OR if there is any other way of doing this feel free to let me know  . 

You know the message box comes up 6 times and shows up the values in descending order with their corresponding letter, now I want to store these 6 values and letters into something else so that I can refer to them later in the code if I want to do any other operations with them. How can I do this? for example it would be good if I could store only numbers , or value1 as you called it, into my 6 variables named best, secondbest, third... etc...


----------



## OBP (Mar 8, 2005)

Sysco, the change you made to the value1() entries has put them in order, the whole point was to have them unsorted so that you could see that the sort worked.
In your code you want to replace the letters "a", "b", "c" etc with lblinputA.Caption, lblinputB.Caption, lblinputC.Caption.
To assign them to new variables you could just delete the message box and put something like this in the for/next loop in it's place- 
value3(x) = value1(x).
Or you could do want you wanted and delete the for/next loop and use 
Best = value1(1)\: bestteam = value2(1)
secondbest = value1(2): secondbestteam = value2(2) etc
which would allow you to use your - 
lblTOTAL.Caption = (best * 50 / 100) + secondbest + thirdbest + fourthbest + (fifthbest * 20 / 100) + (sixthbest * 20 / 100)


----------



## Sysco (Apr 13, 2005)

excellent!, i got it now, sorry i didnt reply earlier the notification email didnt work out... I think I'll be able to finish off my project now! Thanks a lot for help OBP! Keep up the good work!


----------



## OBP (Mar 8, 2005)

Thanks for letting us know Sysco, can you mark the post as "Solved" using the "Thread Tools" at the top of the Forum page.


----------

