| | |
Sorting 3 numbers ascending and descending
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num1, num2, num3 As Integer num1 = TextBox1.Text num2 = TextBox2.Text num3 = TextBox3.Text
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:
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:
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)
Teme64 @ Windows Developer Blog
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
If you want to do it without using an array, try this:
VB.NET Syntax (Toggle Plain Text)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Done As Boolean = False Dim Over As Boolean = False Dim num1 As Single = Single.Parse(TextBox1.Text) Dim num2 As Single = Single.Parse(TextBox2.Text) Dim num3 As Single = Single.Parse(TextBox3.Text) Do While Not Done Done = True If num1 > num2 Then Swap(num1, num2) : Done = False If num1 > num3 Then Swap(num1, num3) : Done = False If num2 > num3 Then Swap(num2, num3) : Done = False Loop TextBox1.Text = num1.ToString TextBox2.Text = num2.ToString TextBox3.Text = num3.ToString End Sub Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single) Dim temp As Single = n1 n1 = n2 n2 = temp End Sub
Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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)
Public Class Form1 Dim MyIntArr(2) As Integer Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Dim num1, num2, num3 As Integer MyIntArr(0) = CInt(TextBox1.Text) MyIntArr(1) = CInt(TextBox2.Text) MyIntArr(2) = CInt(TextBox3.Text) Array.Sort(MyIntArr) outputBox.Text = MyIntArr(2) 'MyIntArr(2) = outputBox.Text End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim i As Integer For i = MyIntArr.GetUpperBound(0) To 0 Step -1 ' Dump MyIntArr(i) Next i outputBox.Text = MyIntArr(i) End Sub End Class
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)
Public Class Form1 Dim MyIntArr(2) As Integer Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Dim num1, num2, num3 As Integer Dim i As Integer MyIntArr(0) = CInt(TextBox1.Text) MyIntArr(1) = CInt(TextBox2.Text) MyIntArr(2) = CInt(TextBox3.Text) Array.Sort(MyIntArr) 'outputBox.Text = MyIntArr(2) 'MyIntArr(2) = outputBox.Text ' Loop the array outputBox.Text = "" For i = 0 To MyIntArr.GetUpperBound(0) outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim i As Integer ' Loop the array outputBox.Text = "" For i = MyIntArr.GetUpperBound(0) To 0 Step -1 ' Dump MyIntArr(i) outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i 'outputBox.Text = MyIntArr(i) End Sub End Class
Teme64 @ Windows Developer Blog
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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)
Public Class Form1 Dim MyIntArr(2) As Integer Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Dim num1, num2, num3 As Integer Dim i As Integer MyIntArr(0) = CInt(TextBox1.Text) MyIntArr(1) = CInt(TextBox2.Text) MyIntArr(2) = CInt(TextBox3.Text) Array.Sort(MyIntArr) 'outputBox.Text = MyIntArr(2) 'MyIntArr(2) = outputBox.Text outputBox.Text = "" For i = 0 To MyIntArr.GetUpperBound(0) outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim i As Integer For i = MyIntArr.GetUpperBound(0) To 0 Step -1 ' Dump MyIntArr(i) outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i 'outputBox.Text = MyIntArr(i) End Sub End Class
Button1 does populate outputbox, right?
Let's fix Button2 code:
Does it work now? If it doesn't, what's in the outputbox after pressing Button2?
Let's fix Button2 code:
VB.NET Syntax (Toggle Plain Text)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim i As Integer MyIntArr(0) = CInt(TextBox1.Text) MyIntArr(1) = CInt(TextBox2.Text) MyIntArr(2) = CInt(TextBox3.Text) Array.Sort(MyIntArr) outputBox.Text = "" For i = MyIntArr.GetUpperBound(0) To 0 Step -1 outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i End Sub
Teme64 @ Windows Developer Blog
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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)
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)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Dim num1, num2, num3 As Integer Dim i As Integer MyIntArr(0) = CInt(TextBox1.Text) MyIntArr(1) = CInt(TextBox2.Text) MyIntArr(2) = CInt(TextBox3.Text) Array.Sort(MyIntArr) 'outputBox.Text = MyIntArr(2) 'MyIntArr(2) = outputBox.Text outputBox.Text = "" For i = 0 To MyIntArr.GetUpperBound(0) outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim i As Integer MyIntArr(0) = CInt(TextBox1.Text) MyIntArr(1) = CInt(TextBox2.Text) MyIntArr(2) = CInt(TextBox3.Text) Array.Sort(MyIntArr) outputBox.Text = "" For i = MyIntArr.GetUpperBound(0) To 0 Step -1 ' Dump MyIntArr(i) outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " Next i 'outputBox.Text = MyIntArr(i) End Sub End Class
I see. You didn't mention it's a listbox
Replace both
lines in Button_Click handlers with
and it works. ListBoxes are populated a bit differently than TextBoxes.
And you clear a ListBox with
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 = "" Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Sorting A Linked List--- Please help (C++)
- Linked Lists C++ Sorting Integers (C++)
- help with sorting program (C++)
- Random number with ascending order help (C++)
- help on sorting...please... (C)
Other Threads in the VB.NET Forum
- Previous Thread: xml to text using vb.net
- Next Thread: Convert HTML file to PDF file through code
| Thread Tools | Search this Thread |
"crystal .net 2008 access add advanced application array assignment basic beginner box browser button buttons center click client code combo convert cpu cuesent data database datagrid datagridview datetimepicker designer dissertation dissertations dissertationtopic dosconsolevb.net eclipse editvb.net employees excel exists firewall forms html images isnumericfuntioncall listview map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity open pan pdf picturebox picturebox2 port print printpreview record regex reuse right-to-left save search serial settings socket sorting sqldatbase sqlserver storedprocedure temp textbox timer transparency txttoxmlconverter upload useraccounts usercontol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic visualbasic.net visualstudio.net web winsock wpf wrapingcode xml





