943,740 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 9199
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 18th, 2008
0

Sorting 3 numbers ascending and descending

Expand Post »
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.

VB Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Nov 19th, 2008
0

Re: Sorting 3 numbers ascending and descending

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)
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 19th, 2008
0

Re: Sorting 3 numbers ascending and descending

If you want to do it without using an array, try this:
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Nov 19th, 2008
0

Re: Sorting 3 numbers ascending and descending

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?

VB Syntax (Toggle Plain 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.  
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Nov 20th, 2008
0

Re: Sorting 3 numbers ascending and descending

You need just a small fix to output values, namely loop the array and append each array element to output text:

VB.NET Syntax (Toggle Plain 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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 20th, 2008
0

Re: Sorting 3 numbers ascending and descending

Thank you, but the outputbox will not populate with the values. I always have problems with populating textboxes. What am I missing?

VB Syntax (Toggle Plain 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.  
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Nov 20th, 2008
0

Re: Sorting 3 numbers ascending and descending

Button1 does populate outputbox, right?
Let's fix Button2 code:
VB.NET Syntax (Toggle Plain Text)
  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?
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 20th, 2008
0

Re: Sorting 3 numbers ascending and descending

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)

VB Syntax (Toggle Plain Text)
  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
Click image for larger version

Name:	clip_image002.jpg
Views:	200
Size:	23.5 KB
ID:	8277  
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Nov 20th, 2008
0

Re: Sorting 3 numbers ascending and descending

BTW, neither button poulates the listbox (outputbox)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Nov 21st, 2008
0

Re: Sorting 3 numbers ascending and descending

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 = ""
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: ListView in VB.Net
Next Thread in VB.NET Forum Timeline: How to Select textbox from string?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC