CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2005
    Posts
    67

    how to sort numbers in a textbox?

    I have numbers that are placed in an array, and displayed in a textbox...
    i want to sort these 10 numbers in an ascending or descending order...but i dont know any command to do this... could anyone explain plz?? thnx...

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to sort numbers in a textbox?

    You can set the SORTED property to True, but that onlyworks if there are the same number of digits in each number. You can format them with zeroes to get them to sort properly, or use a second (hidden) listbox to hold the formatted numbers, and then load them into the first listbox unformatted.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: how to sort numbers in a textbox?

    Quote Originally Posted by dglienna
    You can set the SORTED property to True
    Textbox, not listbox.
    Quote Originally Posted by limpit
    I have numbers that are placed in an array, and displayed in a textbox...
    But the rest of your point is valid.

    @limpit
    You could look into sorting methods and implement one to sort the array before you display it in the text box. There is a whole list of sorting methods and how to implement them in VB here.

    Of course, you could always implement a method like dglienna said.

  4. #4
    Join Date
    Apr 2006
    Posts
    1

    Re: how to sort numbers in a textbox?

    Ok, I'll give it a shot. You can write a function that will return a new sorted number. The "number" in the text box is a string. You can retrieve the string, get the length of the string, get each character and store the integer value of each character in an array. Then sort through the numerical values to determine the ordering of the characters.

    Something like this:

    Function SortedNumber() As String
    Dim EnteredNumber(1 to 200) as Integer
    Dim SortedPositions(1 to 200) as Integer
    Txt$=TextBox.Text 'Get string in text box
    L%=LEN(Txt$) 'length of string
    'Place each character in an array as an integer
    FOR N%=1 to L%
    A$=MID$(Txt$,N%,1) 'Get each "number" character
    EnteredNumber(N%)=Val(A$) 'numerical value of the character
    NEXT N%
    'Now sort the characters
    StartPos%=0
    DO
    StartPos%=StartPos% + 1
    If StartPos%=L% Then
    Exit Do
    End If
    Nmin%=999
    'Find the lowest number in the array
    For N%=StartPos% To L%
    IF EnteredNumber(N%)<Nmin% Then
    Nmin%=EnteredNumber(N%)
    MinPos%=N%
    End If
    Next N%
    SortedPositions(StartPos%)=MinPos%
    EnteredNumber(MinPos%)=1000 'Mark the last lowest number
    LOOP
    'Create the sorted string
    SortedNumber$=""
    For N%=1 to L%
    SortedNumber$=SortedNumber$ + Mid(Txt$,SortedPositions(N%),1)
    Next N%
    End Sub

    I haven't test this one, but I've done this type of thing a lot.
    Last edited by DCEngineer; April 28th, 2006 at 03:39 PM.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to sort numbers in a textbox?

    Missed the Textbox part.
    I guess it made more sense that the numbers were stored in a listbox.
    I assumed one beneath the others, not next to.

    Also, if there is a decimal point, or comma, then this could cause problems, for numbers next to each other.

    Perhaps to just split on a space? I think it'd be better to use a listbox, though.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: how to sort numbers in a textbox?

    Here is a function that I use to sort an array of numbers
    Code:
    'function : sortArray
    'Input : The array that needs to be sorted
    'OutPut : Sorted Array
    'Description : Sorts an array of integers in an ascending order
    '			   To change the sort order just replace the > sign with < sign
    Public Sub sortNumbers(inputNumbers() As Integer)
    	Dim temp As Integer
    	Dim loopCounter1 As Long, loopCounter2 As Long
    	For loopCounter1 = LBound(inputNumbers) To UBound(inputNumbers)
    		For loopCounter2 = loopCounter1 + 1 To UBound(inputNumbers)
    			If inputNumbers(loopCounter1) > inputNumbers(loopCounter2) Then
    				temp = inputNumbers(loopCounter1)
    				inputNumbers(loopCounter1) = inputNumbers(loopCounter2)
    				inputNumbers(loopCounter2) = temp
    			End If
    		Next loopCounter2
    	Next loopCounter1
    End Sub
    Edit ---
    Corrected the input argument according to the below mentioned post..
    Last edited by Shuja Ali; April 30th, 2006 at 04:56 AM.

  7. #7
    Join Date
    Mar 2006
    Posts
    135

    Re: how to sort numbers in a textbox?

    There are a few things wrong with that function.
    1. The variable name used doesn't match the name of the arguement
    2. The lower bound of the array will not always be zero: loopCounter1 should start at LBound(inputNumbers)
    3. The last element of the array is never checked: loopCounter2 should run through UBound(inputNumbers)
    4. This function modifies the array passed to it. If this is desired, the return value is redundant and a Sub would be sufficient. If you do not want to modify the original array, you'll need to copy it and sort the copy.

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: how to sort numbers in a textbox?

    Quote Originally Posted by Logophobic
    There are a few things wrong with that function.
    1. The variable name used doesn't match the name of the arguement
    I have corrected that, thanks for pointing it out. This is what happens when you write code in notepad.
    Quote Originally Posted by Logophobic
    2. The lower bound of the array will not always be zero: loopCounter1 should start at LBound(inputNumbers)
    Thats also right
    Quote Originally Posted by Logophobic
    3. The last element of the array is never checked: loopCounter2 should run through UBound(inputNumbers)
    You are right.
    Quote Originally Posted by Logophobic
    4. This function modifies the array passed to it. If this is desired, the return value is redundant and a Sub would be sufficient. If you do not want to modify the original array, you'll need to copy it and sort the copy.
    Yes arrays always passed by reference so it will modify the original array..

    I have modified my post to reflect the changes that you have suggested..

    Thanks.

  9. #9
    Join Date
    Sep 2005
    Posts
    67

    Re: how to sort numbers in a textbox?

    well...im some sort of noob in this area of using functions...can anyone tell me how to call them...or even use them?

  10. #10
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: how to sort numbers in a textbox?

    Its not all that difficult.

    Imagine you have an array of numbers
    Code:
    Dim someNumbers(5) As Integer
    someNumbers(0) = 10
    someNumbers(1) = 154
    someNumbers(2) = 287
    someNumbers(3) = 2
    someNumbers(4) = 182
    'call the function to sort the numbers
    sortNumbers someNumbers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured