Array.Sort not sorting by character code value?

N

Niklas Uhlin

Why does the code below output this:
"v a" (118,32,97)
"w a" (119,32,97)
"v b" (118,32,98)
"w b" (119,32,98)
"v c" (118,32,99)
"w c" (119,32,99)

and not this (as one would expect if Array.Sort sorted by character
code value):
"v a" (118,32,97)
"v b" (118,32,98)
"v c" (118,32,99)
"w a" (119,32,97)
"w b" (119,32,98)
"w c" (119,32,99)


Is there any way to make Array.Sort sort strings by character code
value?

---------------------------

string[] strings = new string[] {
"w b",
"v a",
"v c",
"w a",
"v b",
"w c"
};

Array.Sort(strings);
PrintStrings(strings);

private void PrintStrings(string[] strings)
{
for (int i=0; i<strings.Length; i++)
{
Console.WriteLine(String.Format(" \"{0}\" ({1})",
strings,
GetASCIICharCodes(strings)));
}
}

private string GetASCIICharCodes(string s)
{
StringBuilder sb = new StringBuilder();

for (int i=0; i<s.Length; i++)
{
char c = Convert.ToChar(s.Substring(i, 1));

sb.Append(String.Format("{0}{1}",
((int)c).ToString(),
i<s.Length-1 ? "," : ""));
}

return sb.ToString();
}

Thanks,
Niklas Uhlin
 
M

Maqsood Ahmed

Hello,
Array.Sort(myArray) method uses default comparer to compare objects. It
gets comparer as Comparer.Default which is a comparer associated with
the Thread.CurrentCulture of the current thread. So it depends on the
current culture how does it behave in this scenario.
I have run your code and it displayed the strings in your desired
format.

Cheers :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
M

Morten Wennevik

Hi Niklas,

As Maqsood said, it is probably caused by your current culture.
As an example, for me, trying to sort the strings

"aaa"
"bbb"
"ccc"

I end up with

"bbb"
"ccc"
"aaa"

because 'aa' in a surname is pronounced as 'å' the last character in the Norwegian alphabet.

Hopefully we will be able to turn off or at least change this behaviour, without having to change culture, in Windows Longhorn.


Similarly you may encountere 'smart sorting' especially for numbers where
"12"
"1A"
is sorted as
"1A"
"12"

because Microsoft has decided that humans in general expect it that way and does not see numeric characters as characters, but numbers and completely different from other characters.

http://blogs.msdn.com/michkap/archive/2005/01/05/346933.aspx

At least you can turn off this behaviour using TweakUI

http://blogs.msdn.com/michkap/archive/2005/04/01/404830.aspx
 
N

Niklas Uhlin

Array.Sort(strings, new Comparer(new
System.Globalization.CultureInfo("sv-SE"))); // Swedish
.... produces the same error.
This is wierd because V comes before W in the swedish alphabet, so one
would think that sorting with a swedish CultureInfo would produce a
correct result.

If I on the other hand use
Array.Sort(myStringArray, new
Comparer(System.Globalization.CultureInfo.InvariantCulture));
.... it works.

Thanks alot for the help!

Brgds,
Niklas Uhlin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top