Can anybody explain why this code:
Dim Data As New SortedList(StringComparer.InvariantCultureIgnoreCase)
Data.Add("AB", 48)
Data.Add("AC", 48)
Data.Add("A-D", 48)
Data.Add("A-", 48)
Generates sorted list with following order:
A-
AB
AC
A-D
Expected (logical and really wanted) order is:
A-
A-D
AB
AC
Dim Data As New SortedList(StringComparer.InvariantCultureIgnoreCase)
I think the problem is with the sort rules specified.
Changing InvariantCultureIgnoreCase
to Ordinal
or OrdinalIgnoreCase
solves the problem
Dim Data As New SortedList(StringComparer.OrdinalIgnoreCase)
Here is the Demo
That is how default string comparer is implemented. To customize this you have to implement your own custom IComparer
or for better compatibility override Comparer<T>
class and pass it to SortedList
constructor or pass StringComparer.OrdinalIgnoreCase
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With