Alexander Petrov
Alexander Petrov

Reputation: 14231

The specificity of sorting

Code of the character '-' is 45, code of the character 'a' is 97. It's clear that '-' < 'a' is true.

Console.WriteLine((int)'-' + " " + (int)'a');
Console.WriteLine('-' < 'a');

45 97
True

Hence the result of the following sort is correct

var a1 = new string[] { "a", "-" };
Console.WriteLine(string.Join(" ", a1));
Array.Sort(a1);
Console.WriteLine(string.Join(" ", a1));

a -
- a

But why the result of the following sort is wrong?

var a2 = new string[] { "ab", "-b" };
Console.WriteLine(string.Join(" ", a2));
Array.Sort(a2);
Console.WriteLine(string.Join(" ", a2));

ab -b
ab -b

Upvotes: 3

Views: 149

Answers (1)

Keith Nicholas
Keith Nicholas

Reputation: 44288

The - is ignored,

so - = "" < a
and -b = "b" > "ab"

this is because of Culture sort ( which is default )

https://msdn.microsoft.com/en-us/library/system.globalization.compareoptions(v=vs.110).aspx

The .NET Framework uses three distinct ways of sorting: word sort, string  

sort, and ordinal sort. Word sort performs a culture-sensitive comparison of strings. Certain nonalphanumeric characters might have special weights assigned to them. For example, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. String sort is similar to word sort, except that there are no special cases. Therefore, all nonalphanumeric symbols come before all alphanumeric characters. Ordinal sort compares strings based on the Unicode values of each element of the string.

Upvotes: 5

Related Questions