10 Replies - 2885 Views - Last Post: 08 October 2012 - 11:35 AM Rate Topic: -----

#1 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

[VB10] Sorting input numerically and alphabetically.

Posted 07 October 2012 - 03:56 PM

Hi, I'm currently learning VB and working on a project of mine.

Goals: trying to sort three numbers in descending order & output the three values in reverse alphabetic order.

Method: Input will be written by the user in 3 separate text-boxes and outputted in a read-only text-box.

My reasoning: [Trying to set up an array that stores the 3 values from each of the three text-boxes. After sorting, output will be prompted in an Output.Text.](http://i.imgur.com/ViAgX.png)

If user selects Numeric button the program should output the three values in numerically descending order.

If User selects Alphabetic button the program should output the three values in reverse alphabetic order.

Here's my current code:


    Option Explicit On
    Option Strict On
    Option Compare Binary
    
    Public Class theMainForm
    
    
        Private Sub NumOrderBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumOrderBtn.Click
    
            Dim num1 As Double 'Three values for each textbox
            Dim num2 As Double
            Dim num3 As Double
    
            num1 = Val(num1Input.Text)    'Val retrieves input from textboxes
            num2 = Val(num2Input.Text)
            num3 = Val(num3Input.Text)
    
            Dim randomNum As New Random()
    
            For i = 0 To 3
    
                numArray(i) = num1Input & num2Input & num3Input
    
                Array.Sort(Array)
    
                Output.Items.Add(numArray(i))
    
            Next
    
        End Sub
    
    End Class


So how can I manage an array by storing values from the text-boxes? Then sort?

Appreciate any guidance given. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: [VB10] Sorting input numerically and alphabetically.

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: [VB10] Sorting input numerically and alphabetically.

Posted 07 October 2012 - 04:08 PM

Have you looked at the other methods of Array? I'm betting there is a .Reverse method in there somewhere.
Was This Post Helpful? 1
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: [VB10] Sorting input numerically and alphabetically.

Posted 07 October 2012 - 11:12 PM

Have you declared numArray somewhere? It is probably this array, you'd like to sort then. Concatenating "a" & "b" gives "ab", and "1" & "2" gives "12". You don't need For... Next loop to add input to numArray. num1Input to 3 are TextBoxes, and you got their Text values already, so use them.
Was This Post Helpful? 1
  • +
  • -

#4 dotINSolution  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 04:56 AM

Some silly mistakes in their.

numArray(i) = num1Input & num2Input & num3Input

You are assigning same number to all items in the array. So no sorting. Also Sorting is being done inside for each loop, not really a need to do so. You can also shorten the code a lot also

Try this, not tested but probably should work:

    Private Sub NumOrderBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumOrderBtn.Click

        numArray(0) = Val(num1Input.Text)    'Val retrieves input from textboxes
        numArray(1) = Val(num2Input.Text)
        numArray(2) = Val(num3Input.Text)

        Array.Sort(numArray)
		
		For each item As String in numArray
			Output.Items.Add(item)
		Next

    End Sub



Similar to Array.Sort for sorting, you can use Array.Reverse to reverse the items in an array.
Hope this helps
Was This Post Helpful? 1
  • +
  • -

#5 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 08:42 AM

View PostCharlieMay, on 07 October 2012 - 04:08 PM, said:

Have you looked at the other methods of Array? I'm betting there is a .Reverse method in there somewhere.


My instructor has yet to teach us about Arrays, but I just didn't know what reverse alphabetic order meant since the user is inputting only numbers. I don't think the array is supposed to be reversed. Does it involve the ANSI?

View Postlucky3, on 07 October 2012 - 11:12 PM, said:

Have you declared numArray somewhere? It is probably this array, you'd like to sort then. Concatenating "a" & "b" gives "ab", and "1" & "2" gives "12". You don't need For... Next loop to add input to numArray. num1Input to 3 are TextBoxes, and you got their Text values already, so use them.


I've declared my array, but after messing with the code, it's not compiling.


View PostdotINSolution, on 08 October 2012 - 04:56 AM, said:

Some silly mistakes in their.

numArray(i) = num1Input & num2Input & num3Input

You are assigning same number to all items in the array. So no sorting. Also Sorting is being done inside for each loop, not really a need to do so. You can also shorten the code a lot also

Try this, not tested but probably should work:

    Private Sub NumOrderBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumOrderBtn.Click

        numArray(0) = Val(num1Input.Text)    'Val retrieves input from textboxes
        numArray(1) = Val(num2Input.Text)
        numArray(2) = Val(num3Input.Text)

        Array.Sort(numArray)
		
		For each item As String in numArray
			Output.Items.Add(item)
		Next

    End Sub




Similar to Array.Sort for sorting, you can use Array.Reverse to reverse the items in an array.
Hope this helps


Appreciate the help from all of you. I will need to reconsider my code.

Is there a way to sort numbers using If statements? I know it's brute force, but I'm comfortable with If statements and I think I can make it work.

Something like this...
If (num1 > num2) And (num1 > num3) And (num2 > num3) Then
            Output.Text = num1 & num2 & num3


Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 08:49 AM

And how are you going to apply that approach to a collection of unknown (at compile tome) number of elements

Numeric Ordering

1,2,3,4,5,6,7,8,9,10,11

Alphabetical Ordering

1,10,11,2,3,4,5,6,7,8,9
Was This Post Helpful? 1
  • +
  • -

#7 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 09:02 AM

View PostAdamSpeight2008, on 08 October 2012 - 08:49 AM, said:

And how are you going to apply that approach to a collection of unknown (at compile tome) number of elements

Numeric Ordering

1,2,3,4,5,6,7,8,9,10,11

Alphabetical Ordering

1,10,11,2,3,4,5,6,7,8,9


Through three text-boxes; user will input only three values that will need to be sorted.

Although, I'll have to think of each possibility of the three numbers being relatively greater/less/equal.

Form:

Posted Image

Here's what I have now:

Option Explicit On
Option Strict On
Option Infer Off
Option Compare Binary

Public Class theMainForm

    Private Sub numOrderBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numOrderBtn.Click

        'Subrountine of Numeric button/object
        'NumOrderBtn as identifier for Numeric button.


        Dim num1 As Double 'Three values for each textbox
        Dim num2 As Double
        Dim num3 As Double


        num1 = CDbl(userInput1.Text)    'Cdbl converts input values to Doubles 
        num2 = CDbl(userInput2.Text)
        num3 = CDbl(userInput3.Text)
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        If (num1 > num2) And (num1 > num3) And (num2 > num3) Then
            Output.Text = num1 & "," & num2 & "," & num3

        ElseIf (num2 > num1) And (num2 > num3) and  Then
            Output.Text = CStr(CDbl(num2))

        ElseIf (num3 > num1) And (num3 > num2) Then
            Output.Text = CStr(CDbl(num3))
        End If

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'If (num1 > Val(num2 & num3)) and ( Then
        'userInput2.Text = CStr(CDbl(num1))

        '  ElseIf (num2 > Val(num1 & num3)) Then
        ' userInput2.Text = CStr(CDbl(num2))

        '  ElseIf (num3 > Val(num1 & num2)) Then
        '  userInput2.Text = CStr(CDbl(num3))
        ' End If
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    End Sub
End Class




Thanks for your guidance on the ordering.

I wasn't too sure what alphabetical ordering was, I'll try to reverse it soon.

That portion will come later after I complete numeric ordering.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 09:03 AM

For a basic understanding of collection I suggest the following C# tutorial. Even though the programming used is C# conceptually it is the same for vb.net, what differ is the syntax.
Was This Post Helpful? 1
  • +
  • -

#9 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 10:23 AM

View PostAdamSpeight2008, on 08 October 2012 - 09:03 AM, said:

For a basic understanding of collection I suggest the following C# tutorial. Even though the programming used is C# conceptually it is the same for vb.net, what differ is the syntax.


Okay, I have started from scratch again and utilized LINQ to help me sort in descending direction.

I think I'm on the right path; however, I am having issues assigning my text-boxes to the number array.

How can I do this?


Public Class Form1

    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click

 
        Dim nums() As Integer = numb1, numb2, numb3

        numb1 = Val(userNum1.Text)    'Cdbl converts input values to Doubles 
        numb2 = Val(userNum2.Text)
        numb3 = Val(userNum3.Text)

        Dim numQuery = From num In nums
                        Order By num Descending
                        Select num

        For Each n As Integer In numQuery
            Output.Items.Add(n)


        Next
    End Sub

End Class



Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 10:30 AM

I think the point of the exercise is to get you thinking about the algorithm for the sorting. Not to use the built in sorting routine or LINQ.
Was This Post Helpful? 1
  • +
  • -

#11 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: [VB10] Sorting input numerically and alphabetically.

Posted 08 October 2012 - 11:35 AM

View PostAdamSpeight2008, on 08 October 2012 - 10:30 AM, said:

I think the point of the exercise is to get you thinking about the algorithm for the sorting. Not to use the built in sorting routine or LINQ.


My professor actually wanted us to experiment with If statements in regards to this VB project.

I tried that method, but it was very tedious so I'm currently working with alternatives.

We haven't really covered arrays just yet, but I thought it would be neat to try that's all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1