CaseInsensitiveComparer weirdness

  • Thread starter Thread starter Paul E Collins
  • Start date Start date
P

Paul E Collins

Shouldn't the CaseInsensitiveComparer sort case-insensitively?

The following code prints out 'AZaz', which indicates a case-sensitive sort.
What do I need to do to perform a case-insensitive sort ('AaZz')?

char[] chars = new char[] { 'A', 'z', 'a', 'Z' };
string sorted = "";

Array.Sort(chars, new System.Collections.CaseInsensitiveComparer());

for (int i = 0; i < chars.Length; i++)
{
sorted += chars;
}

Console.WriteLine(sorted);
 
Paul E Collins said:
Shouldn't the CaseInsensitiveComparer sort case-insensitively?

It should, and it does - for strings. It doesn't compare characters. If
you change your sample to use strings instead of chars, it works fine.
 
Jon Skeet said:
It should, and it does - for strings. It doesn't
compare characters. If you change your sample
to use strings instead of chars, it works fine.

Ah, thanks for clearing that up!

P.
 
Back
Top