-
April 28th, 2006, 12:09 PM
#1
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...
-
April 28th, 2006, 01:08 PM
#2
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.
-
April 28th, 2006, 01:30 PM
#3
Re: how to sort numbers in a textbox?
Originally Posted by dglienna
You can set the SORTED property to True
Textbox, not listbox.
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.
-
April 28th, 2006, 01:59 PM
#4
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.
-
April 28th, 2006, 05:43 PM
#5
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.
-
April 29th, 2006, 04:58 AM
#6
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.
-
April 29th, 2006, 09:34 AM
#7
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.
-
April 30th, 2006, 04:50 AM
#8
Re: how to sort numbers in a textbox?
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.
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
Originally Posted by Logophobic
3. The last element of the array is never checked: loopCounter2 should run through UBound(inputNumbers)
You are right.
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.
-
May 2nd, 2006, 02:18 PM
#9
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?
-
May 3rd, 2006, 01:23 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|