Sorting 3 numbers ascending and descending

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Sorting 3 numbers ascending and descending

 
0
  #1
Nov 18th, 2008
I need to sort three numbers that are entered in three different textboxes and display these numbers ascending and when I press a different button display them descending. Now I think that I need to put these three numbers into an array, sort and display, but I'm not really too proficient in code to accomplish this. Is there a way to do this without putting them into an Array. Here is my code so far? Which is not much of anything so far. Any guidance would be greatly appreciated, Thanks in advance.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim num1, num2, num3 As Integer
  3.  
  4. num1 = TextBox1.Text
  5. num2 = TextBox2.Text
  6. num3 = TextBox3.Text
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Sorting 3 numbers ascending and descending

 
0
  #2
Nov 19th, 2008
You can sort three numbers without an array. But your prof/teacher may have wanted an array-based solution. So put those numbers in an array and you have a solution which can easily handle also four integer's sorting .

VB.NET arrays have a Sort-method: Array.Sort(MyIntArr) which sorts your array in ascending order and that's all you need.

In the second button (descending order) , simply dump your sorted array out in the reverse order:
For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  ' Dump MyIntArr(i)
Next i

One last thing. You can't directly assign textual value to integer, you have to cast the textual representation of the number to the actual numerical value:
Dim MyIntArr(2) As Integer
MyIntArr(0) = CInt(TextBox1.Text)
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Sorting 3 numbers ascending and descending

 
0
  #3
Nov 19th, 2008
If you want to do it without using an array, try this:
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim Done As Boolean = False
  3. Dim Over As Boolean = False
  4. Dim num1 As Single = Single.Parse(TextBox1.Text)
  5. Dim num2 As Single = Single.Parse(TextBox2.Text)
  6. Dim num3 As Single = Single.Parse(TextBox3.Text)
  7. Do While Not Done
  8. Done = True
  9. If num1 > num2 Then Swap(num1, num2) : Done = False
  10. If num1 > num3 Then Swap(num1, num3) : Done = False
  11. If num2 > num3 Then Swap(num2, num3) : Done = False
  12. Loop
  13. TextBox1.Text = num1.ToString
  14. TextBox2.Text = num2.ToString
  15. TextBox3.Text = num3.ToString
  16. End Sub
  17.  
  18. Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single)
  19. Dim temp As Single = n1
  20. n1 = n2
  21. n2 = temp
  22. End Sub
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: Sorting 3 numbers ascending and descending

 
0
  #4
Nov 19th, 2008
Ok, thanks for the help, any further suggestions on how to get this to work would be appreciated. Here is my code so far, I'm not sure what I'm doing wrong?

  1. Public Class Form1
  2. Dim MyIntArr(2) As Integer
  3. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  4. Me.Close()
  5.  
  6. End Sub
  7.  
  8. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9. 'Dim num1, num2, num3 As Integer
  10.  
  11. MyIntArr(0) = CInt(TextBox1.Text)
  12. MyIntArr(1) = CInt(TextBox2.Text)
  13. MyIntArr(2) = CInt(TextBox3.Text)
  14. Array.Sort(MyIntArr)
  15.  
  16. outputBox.Text = MyIntArr(2)
  17. 'MyIntArr(2) = outputBox.Text
  18.  
  19. End Sub
  20.  
  21. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  22. Dim i As Integer
  23.  
  24. For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  25. ' Dump MyIntArr(i)
  26. Next i
  27. outputBox.Text = MyIntArr(i)
  28. End Sub
  29. End Class
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Sorting 3 numbers ascending and descending

 
0
  #5
Nov 20th, 2008
You need just a small fix to output values, namely loop the array and append each array element to output text:

  1. Public Class Form1
  2. Dim MyIntArr(2) As Integer
  3. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  4. Me.Close()
  5.  
  6. End Sub
  7.  
  8. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9. 'Dim num1, num2, num3 As Integer
  10. Dim i As Integer
  11.  
  12. MyIntArr(0) = CInt(TextBox1.Text)
  13. MyIntArr(1) = CInt(TextBox2.Text)
  14. MyIntArr(2) = CInt(TextBox3.Text)
  15. Array.Sort(MyIntArr)
  16.  
  17. 'outputBox.Text = MyIntArr(2)
  18. 'MyIntArr(2) = outputBox.Text
  19.  
  20. ' Loop the array
  21. outputBox.Text = ""
  22. For i = 0 To MyIntArr.GetUpperBound(0)
  23. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  24. Next i
  25.  
  26. End Sub
  27.  
  28. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  29. Dim i As Integer
  30.  
  31. ' Loop the array
  32. outputBox.Text = ""
  33. For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  34. ' Dump MyIntArr(i)
  35. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  36. Next i
  37. 'outputBox.Text = MyIntArr(i)
  38. End Sub
  39. End Class
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: Sorting 3 numbers ascending and descending

 
0
  #6
Nov 20th, 2008
Thank you, but the outputbox will not populate with the values. I always have problems with populating textboxes. What am I missing?

  1. Public Class Form1
  2. Dim MyIntArr(2) As Integer
  3. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  4. Me.Close()
  5.  
  6. End Sub
  7.  
  8. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9. 'Dim num1, num2, num3 As Integer
  10. Dim i As Integer
  11.  
  12. MyIntArr(0) = CInt(TextBox1.Text)
  13. MyIntArr(1) = CInt(TextBox2.Text)
  14. MyIntArr(2) = CInt(TextBox3.Text)
  15.  
  16. Array.Sort(MyIntArr)
  17.  
  18. 'outputBox.Text = MyIntArr(2)
  19. 'MyIntArr(2) = outputBox.Text
  20.  
  21. outputBox.Text = ""
  22. For i = 0 To MyIntArr.GetUpperBound(0)
  23.  
  24. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  25.  
  26. Next i
  27.  
  28. End Sub
  29.  
  30. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  31. Dim i As Integer
  32.  
  33. For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  34. ' Dump MyIntArr(i)
  35. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  36. Next i
  37. 'outputBox.Text = MyIntArr(i)
  38. End Sub
  39. End Class
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Sorting 3 numbers ascending and descending

 
0
  #7
Nov 20th, 2008
Button1 does populate outputbox, right?
Let's fix Button2 code:
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. Dim i As Integer
  3.  
  4. MyIntArr(0) = CInt(TextBox1.Text)
  5. MyIntArr(1) = CInt(TextBox2.Text)
  6. MyIntArr(2) = CInt(TextBox3.Text)
  7.  
  8. Array.Sort(MyIntArr)
  9.  
  10. outputBox.Text = ""
  11. For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  12. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  13. Next i
  14.  
  15. End Sub
Does it work now? If it doesn't, what's in the outputbox after pressing Button2?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: Sorting 3 numbers ascending and descending

 
0
  #8
Nov 20th, 2008
still doesn't populate, does it matter that the outputbox is a listbox?
button1 should display numbers in list box in ascending order when pressed
button2 should display numbers in list box in descending order when pressed

see attached pic of my form (might be hard to see)

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. 'Dim num1, num2, num3 As Integer
  3. Dim i As Integer
  4.  
  5. MyIntArr(0) = CInt(TextBox1.Text)
  6. MyIntArr(1) = CInt(TextBox2.Text)
  7. MyIntArr(2) = CInt(TextBox3.Text)
  8.  
  9. Array.Sort(MyIntArr)
  10.  
  11. 'outputBox.Text = MyIntArr(2)
  12. 'MyIntArr(2) = outputBox.Text
  13.  
  14. outputBox.Text = ""
  15. For i = 0 To MyIntArr.GetUpperBound(0)
  16.  
  17. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  18.  
  19. Next i
  20.  
  21. End Sub
  22.  
  23. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  24. Dim i As Integer
  25.  
  26. MyIntArr(0) = CInt(TextBox1.Text)
  27. MyIntArr(1) = CInt(TextBox2.Text)
  28. MyIntArr(2) = CInt(TextBox3.Text)
  29.  
  30. Array.Sort(MyIntArr)
  31.  
  32. outputBox.Text = ""
  33. For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  34. ' Dump MyIntArr(i)
  35. outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
  36. Next i
  37. 'outputBox.Text = MyIntArr(i)
  38. End Sub
  39. End Class
Attached Thumbnails
clip_image002.jpg  
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: Sorting 3 numbers ascending and descending

 
0
  #9
Nov 20th, 2008
BTW, neither button poulates the listbox (outputbox)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Sorting 3 numbers ascending and descending

 
0
  #10
Nov 21st, 2008
I see. You didn't mention it's a listbox

Replace both
outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
lines in Button_Click handlers with
outputBox.Items.Add(MyIntArr(i))
and it works. ListBoxes are populated a bit differently than TextBoxes.
And you clear a ListBox with outputBox.Items.Clear instead of outputBox.Text = ""
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC