Results 1 to 20 of 20

Thread: Listbox Sorting - Numbers *[NOW RESOLVED]*

  1. #1

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435

    Thumbs up Listbox Sorting - Numbers *[NOW RESOLVED]*

    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?
    Last edited by RealNickyDude; May 8th, 2004 at 06:02 AM.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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)

  3. #3

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    I added the numbers through the Items - (collection) Property, this must add them as strings.

    Then I tried
    Code:
            With lstSectionList
                .Items.Add("<NEW>")
                .Items.Add(112)
                .Items.Add(32)
                .Items.Add(102)
                .Items.Add(11)
                .Items.Add(53)
            End With
    As there's a mixture of string and numbers, and it came out as:

    102
    11
    112
    32
    53

    Not the way I wanted them sorted. Any more help?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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:
    1. ListBox1.Items.Add(Format(5, "00000#"))
    2.         ListBox1.Items.Add(Format(1125, "00000#"))
    3.         ListBox1.Items.Add(Format(865, "00000#"))

  6. #6

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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:
    1. ListBox1.Items.Add(Format(5, "00000#"))
    2.         ListBox1.Items.Add(Format(1125, "00000#"))
    3.         ListBox1.Items.Add(Format(865, "00000#"))
    That worked, thanks. but is there anyway to sort the numbers without having the leading zero's?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    See if this helps, I wrote it right now, so it might be buggy.

    VB Code:
    1. Sub SortListBox(ByRef box As ListBox)
    2.  
    3.  
    4.         Dim myarray(box.Items.Count) As Integer
    5.         Dim i As Integer
    6.         Dim j As Integer = 0
    7.         Dim intTemp As Integer
    8.  
    9.  
    10.  
    11.         For i = 0 To box.Items.Count - 1
    12.             myarray(i) = CInt(box.Items(i))
    13.         Next
    14.  
    15.         myarray.Sort(myarray)
    16.  
    17.         i = 0
    18.  
    19.  
    20.  
    21.         Do While i < UBound(myarray)
    22.             Do While j < UBound(myarray) - i
    23.                 If CInt(myarray(j + 1)) < CInt(myarray(j)) Then
    24.                     intTemp = CInt(myarray(j))
    25.                     myarray(j) = CInt(myarray(j + 1))
    26.                     myarray(j + 1) = CInt(intTemp)
    27.                 End If
    28.                 j += 1
    29.             Loop
    30.             j = 0
    31.             i += 1
    32.  
    33.         Loop
    34.  
    35.         '  array1.Sort(i)
    36.  
    37.         box.Items.Clear()
    38.         For i = UBound(myarray) To 0 Step -1
    39.             box.Items.Add(myarray(i))
    40.         Next
    41.     End Sub

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You would have to have the sorted property set to false and sort the list manually. Then insert the items from your sort.

  9. #9

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Originally posted by mendhak
    See if this helps, I wrote it right now, so it might be buggy.

    VB Code:
    1. Sub SortListBox(ByRef box As ListBox)
    2.  
    3.  
    4.         Dim myarray(box.Items.Count) As Integer
    5.         Dim i As Integer
    6.         Dim j As Integer = 0
    7.         Dim intTemp As Integer
    8.  
    9.  
    10.  
    11.         For i = 0 To box.Items.Count - 1
    12.             myarray(i) = CInt(box.Items(i))
    13.         Next
    14.  
    15.         myarray.Sort(myarray)
    16.  
    17.         i = 0
    18.  
    19.  
    20.  
    21.         Do While i < UBound(myarray)
    22.             Do While j < UBound(myarray) - i
    23.                 If CInt(myarray(j + 1)) < CInt(myarray(j)) Then
    24.                     intTemp = CInt(myarray(j))
    25.                     myarray(j) = CInt(myarray(j + 1))
    26.                     myarray(j + 1) = CInt(intTemp)
    27.                 End If
    28.                 j += 1
    29.             Loop
    30.             j = 0
    31.             i += 1
    32.  
    33.         Loop
    34.  
    35.         '  array1.Sort(i)
    36.  
    37.         box.Items.Clear()
    38.         For i = UBound(myarray) To 0 Step -1
    39.             box.Items.Add(myarray(i))
    40.         Next
    41.     End Sub
    Being a newbi, this is a bit over my head so I think I'll stick with the '00' format. Thanks.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.
    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!

  11. #11
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    No need to write your own sort:

    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
    Just make sure the sorted property on the listbox is false.

  12. #12

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Thanks, that seemed to work ok.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  13. #13

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Well, I thought it worked

    Here's my code for adding the text from a textbox
    VB Code:
    1. Private Sub txtAddSection_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    2.  Handles txtAddSection.KeyDown
    3.         If e.KeyCode = 13 Then
    4.             boxArr.Add(txtAddSection.Text) 'Add text into the array
    5.             ListSort() 'Sort the array
    6.             txtAddSection.Clear() 'Clear text ready for next one
    7.         End If
    8.     End Sub
    and here's the sort
    VB Code:
    1. Private Sub ListSort()
    2.         ' This sorts the Section numbers into the correct order
    3.         ' and places the result in the listbox.
    4.         boxArr.Sort()
    5.         Dim i As Int16
    6.         lstSectionList.Items.Clear() 'Clear list
    7.         For i = 0 To boxArr.Count - 1
    8.             'lstSectionList.Items.Add(Format(boxArr(i), "00#"))
    9.             lstSectionList.Items.Add(boxArr(i))
    10.         Next
    11.     End Sub
    but instead of

    4
    12
    25
    33
    88
    100
    300


    it comes out as

    100
    12
    25
    300
    33
    4
    88


    I tried formatting the output
    Code:
    lstSectionList.Items.Add(Format(boxArr(i), "00#"))
    but all that comes out, no matter what number I type in is 00#.

    Last edited by RealNickyDude; May 8th, 2004 at 05:28 PM.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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

    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  15. #15

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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,
    Code:
     boxArr.Add(txtAddSection.Text) 'Add text into the array
    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.

    I've now tried
    Code:
    Convert.ToInt16(txtAddSection.Text)
    but to no avail, how do I convert a textbox string to an integer?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  16. #16
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  17. #17

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Val, of course, doh!

    Yup, all works correctly now, thanks for taking the time to help a newbie
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  18. #18
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224
    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

  19. #19

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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
    Exactly what have yulyos
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  20. #20
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224
    Hi,

    Look at the time we post the reply

    Have a nice day

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