Results 1 to 7 of 7

Thread: listbox sorting wrong

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    193

    listbox sorting wrong

    Im in Vb.net and it took a little getting used to the new listbox property, but I got it now. I am storing self created objects that contain a key and a string. THe problem is that the key is not sorting,
    The values are something like

    3E-1
    3E-11
    3E-12
    3E-2

    I have tried putting it into an arraylist first, then sorting, but same order. Any ideas??
    thks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: listbox sorting wrong

    What's wrong with that sorting? The sort order that you show are what you would expect for strings being sorted alphabetically. If you were expecting the sort order to be:

    3E-1
    3E-2
    3E-11
    3E-12

    then you are going to have to have a custom comparator function for the sort routine to call.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: listbox sorting wrong

    The string "11" comes before the string "2", just like the string "aa" comes before the string "b". Sorting in the ListBox is done using the character values, not the number values (if there are numbers). Simply put, the ListBox looks at the characters one by one and decides that "1" comes before "2", even if there is another "1" after the first "1"! It doesn't know it's actually the number 11.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox sorting wrong

    which version of vb.net are you using?

    what if you have 10E-1, that wouldn't sort properly either?

    if you're using vb2008, you can sort your listitems with LINQ

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: listbox sorting wrong

    The documentation for Listbox.Sort includes an example of overriding the Sort method to provide for custom sorting. Alternatively, you could sort the information in a list, then use AddRange to add the sorted information to the Listbox. You would then want to leave the Listbox.Sorted property set to False so that the Listbox didn't re-sort the strings.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    193

    Re: listbox sorting wrong

    Thank you all for so many replies. I suppose upon reflection, it is sorting properly according to text rules. In answer , 10E-1 does sort properly after 9. the problem is with the 2nd part of the field. I can sort them separately and then regroup them manually, if I must

    Thanks again

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox sorting wrong

    try this:

    vb Code:
    1. Dim items(ListBox2.Items.Count - 1) As String
    2. ListBox2.Items.CopyTo(items, 0)
    3. Array.Sort(items, New comparer2)
    4. ListBox2.Items.Clear()
    5. ListBox2.Items.AddRange(items)

    this is the comparer class:

    vb Code:
    1. Public Class comparer2
    2.     Implements IComparer
    3.  
    4.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    5.         If x.ToString.Split("-"c)(0) = y.ToString.Split("-"c)(0) Then
    6.             Return CInt(x.ToString.Split("-"c)(1)).CompareTo(CInt(y.ToString.Split("-"c)(1)))
    7.         Else
    8.             Return x.ToString.Split("-"c)(0).CompareTo(y.ToString.Split("-"c)(0))
    9.         End If
    10.     End Function
    11.  
    12. End Class

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