How do you sort numbers in a listbox? I have the sorted property to true but the numbers come out like:
102
21
231
34
536
55
instead of
21
34
55
102
231
536
How do I sort these into correct numerical order?
Printable View
How do you sort numbers in a listbox? I have the sorted property to true but the numbers come out like:
102
21
231
34
536
55
instead of
21
34
55
102
231
536
How do I sort these into correct numerical order?
Are you adding them as strings, or as numbers?
There's a difference between adding them like this:
ListBox1.Items.Add("112")
and
ListBox1.Items.Add(112)
I added the numbers through the Items - (collection) Property, this must add them as strings.
Then I tried
As there's a mixture of string and numbers, and it came out as:Code:With lstSectionList
.Items.Add("<NEW>")
.Items.Add(112)
.Items.Add(32)
.Items.Add(102)
.Items.Add(11)
.Items.Add(53)
End With
102
11
112
32
53
Not the way I wanted them sorted. Any more help? :D
I must be going crazy, I ran an example when I saw your question first, and I could have sworn it worked for me.
Looks like...You'll have to write a subroutine for this.
The easiest way to do this would be to format the numbers, so that you have leading zeros on the numbers. Like so:
VB Code:
ListBox1.Items.Add(Format(5, "00000#")) ListBox1.Items.Add(Format(1125, "00000#")) ListBox1.Items.Add(Format(865, "00000#"))
That worked, thanks. but is there anyway to sort the numbers without having the leading zero's?Quote:
Originally posted by Negative0
The easiest way to do this would be to format the numbers, so that you have leading zeros on the numbers. Like so:
VB Code:
ListBox1.Items.Add(Format(5, "00000#")) ListBox1.Items.Add(Format(1125, "00000#")) ListBox1.Items.Add(Format(865, "00000#"))
See if this helps, I wrote it right now, so it might be buggy.
VB Code:
Sub SortListBox(ByRef box As ListBox) Dim myarray(box.Items.Count) As Integer Dim i As Integer Dim j As Integer = 0 Dim intTemp As Integer For i = 0 To box.Items.Count - 1 myarray(i) = CInt(box.Items(i)) Next myarray.Sort(myarray) i = 0 Do While i < UBound(myarray) Do While j < UBound(myarray) - i If CInt(myarray(j + 1)) < CInt(myarray(j)) Then intTemp = CInt(myarray(j)) myarray(j) = CInt(myarray(j + 1)) myarray(j + 1) = CInt(intTemp) End If j += 1 Loop j = 0 i += 1 Loop ' array1.Sort(i) box.Items.Clear() For i = UBound(myarray) To 0 Step -1 box.Items.Add(myarray(i)) Next End Sub
You would have to have the sorted property set to false and sort the list manually. Then insert the items from your sort.
Being a newbi, this is a bit over my head so I think I'll stick with the '00' format. Thanks.Quote:
Originally posted by mendhak
See if this helps, I wrote it right now, so it might be buggy.
VB Code:
Sub SortListBox(ByRef box As ListBox) Dim myarray(box.Items.Count) As Integer Dim i As Integer Dim j As Integer = 0 Dim intTemp As Integer For i = 0 To box.Items.Count - 1 myarray(i) = CInt(box.Items(i)) Next myarray.Sort(myarray) i = 0 Do While i < UBound(myarray) Do While j < UBound(myarray) - i If CInt(myarray(j + 1)) < CInt(myarray(j)) Then intTemp = CInt(myarray(j)) myarray(j) = CInt(myarray(j + 1)) myarray(j + 1) = CInt(intTemp) End If j += 1 Loop j = 0 i += 1 Loop ' array1.Sort(i) box.Items.Clear() For i = UBound(myarray) To 0 Step -1 box.Items.Add(myarray(i)) Next End Sub
Uhm... it's very simple code, working with arrays. Just the bubble sort. I'm a newbie too, sorry if that was too difficult though. I'm with you on the VB.NET learning path! :afrog:Quote:
Originally posted by RealNickyDude
Being a newbi, this is a bit over my head so I think I'll stick with the '00' format. Thanks.
No need to write your own sort:
Just make sure the sorted property on the listbox is false.Code:Dim boxArr As New ArrayList
boxArr.Add(15)
boxArr.Add(125)
boxArr.Add(715)
boxArr.Add(1)
boxArr.Sort()
Dim i As Int16
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
Thanks, that seemed to work ok.
Well, I thought it worked :(
Here's my code for adding the text from a textbox
and here's the sortVB Code:
Private Sub txtAddSection_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAddSection.KeyDown If e.KeyCode = 13 Then boxArr.Add(txtAddSection.Text) 'Add text into the array ListSort() 'Sort the array txtAddSection.Clear() 'Clear text ready for next one End If End Subbut instead ofVB Code:
Private Sub ListSort() ' This sorts the Section numbers into the correct order ' and places the result in the listbox. boxArr.Sort() Dim i As Int16 lstSectionList.Items.Clear() 'Clear list For i = 0 To boxArr.Count - 1 'lstSectionList.Items.Add(Format(boxArr(i), "00#")) lstSectionList.Items.Add(boxArr(i)) Next End Sub
4
12
25
33
88
100
300
it comes out as
100
12
25
300
33
4
88
I tried formatting the output
but all that comes out, no matter what number I type in is 00#.Code:lstSectionList.Items.Add(Format(boxArr(i), "00#"))
:(
Hi,
"Are you adding them as strings, or as numbers?
There's a difference between adding them like this:
ListBox1.Items.Add("112")
and
ListBox1.Items.Add(112)"
Surely anything added to a listbox or a combobox is accepted as a string??? Therefore, a numerical sort is not possible. The data must be sorted numerically before adding to the box.
Negative's posted code DOES work.
" Dim boxArr As New ArrayList
boxArr.Add(15)
boxArr.Add(125)
boxArr.Add(715)
boxArr.Add(1)
boxArr.Sort()
Dim i As Int16
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next"
BUT YOU MUST SET THE LISTBOX SORTED PROPERTY TO FALSE
:wave:
Thanks for the reply Taxes,
The Sorted property is set to false :)
I think i'm getting it now, I'm adding the numbers through a text box,
so I guess this is adding the numbers as strings. I need to convert the number from a textbox to an integer before adding it to the array.Code:boxArr.Add(txtAddSection.Text) 'Add text into the array
I've now tried
but to no avail, how do I convert a textbox string to an integer?Code:Convert.ToInt16(txtAddSection.Text)
Hi,
Presumeably you are using one text box into which you insert the number and then use a button to accept the entry.
In the button click event
boxArr.add(val(txtBox.text)
then when ready
boxArr.Sort()
Dim i As Int16
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
Val, of course, doh!
Yup, all works correctly now, thanks for taking the time to help a newbie :thumb:
Hi,
Dim boxArr As New ArrayList()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = 13 Then
boxArr.Add(Val((TextBox1.Text))) 'Add text into the array
ListSort() 'Sort the array
TextBox1.Clear() 'Clear text ready for next one
End If
End Sub
Private Sub ListSort()
boxArr.Sort()
Dim i As Int16
ListBox1.Items.Clear() 'Clear list
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
End Sub
Have a nice day
Exactly what have yulyos :DQuote:
Originally posted by yulyos
Hi,
Dim boxArr As New ArrayList()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = 13 Then
boxArr.Add(Val((TextBox1.Text))) 'Add text into the array
ListSort() 'Sort the array
TextBox1.Clear() 'Clear text ready for next one
End If
End Sub
Private Sub ListSort()
boxArr.Sort()
Dim i As Int16
ListBox1.Items.Clear() 'Clear list
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
End Sub
Have a nice day
Hi,
Look at the time we post the reply
Have a nice day