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.
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.
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
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.
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
Re: listbox sorting wrong
try this:
vb Code:
Dim items(ListBox2.Items.Count - 1) As String
ListBox2.Items.CopyTo(items, 0)
Array.Sort(items, New comparer2)
ListBox2.Items.Clear()
ListBox2.Items.AddRange(items)
this is the comparer class:
vb Code:
Public Class comparer2
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
If x.ToString.Split("-"c)(0) = y.ToString.Split("-"c)(0) Then
Return CInt(x.ToString.Split("-"c)(1)).CompareTo(CInt(y.ToString.Split("-"c)(1)))
Else
Return x.ToString.Split("-"c)(0).CompareTo(y.ToString.Split("-"c)(0))
End If
End Function
End Class