| | |
Sorting 3 numbers ascending and descending
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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
Views: 2959 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2005 2008 access account application array arrays basic bing button buttons c# center check checkbox code convert crystalreport data database datagrid datagridview date design designer dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy images inline input insert installer intel internet listview mobile monitor net networking objects output panel passingparameters picturebox port position print printing problem read remove save searchbox searchvb.net select serial shutdown soap sorting studio survey table tcp temperature text textbox time timer timespan toolbox trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet visual visualbasic visualbasic.net visualstudio2008 web webbrowser winforms wpf year





