Search code examples
vb.netsortingsorteddictionary

SortedDictionary behavior


I'm using a SortedDictonary(Of String, String) in my application, and I experience a strange sorting behavior. Consider the following code example:

Dim Dic As New SortedDictionary(Of String, String)
Dic.Add("'A", "")
Dic.Add("A", "")
Dic.Add("'B", "")
Dic.Add("B", "")
Dic.Add("'C", "")
Dic.Add("C", "")

I would expect the keys to be sorted as "'A", "'B", "'C", "A", "B", "C", which is what you get when comparing the keys "by hand", through the < operator. And yet, iterating through the keys returns "A", "'A", "B", "'B", "C", "'C".

How can I change the SortedDictionary behavior to sort the words beginning with ' first?

Thank you, CFP


Solution

  • In my phone book, John O'Leary sorts between Ole Tractors and Dennis Oleck. You'll have to use a non-culture dependent sort:

    Dim Dic As New SortedDictionary(Of String, String)(StringComparer.Ordinal)