Why does .NET sort the characters ‘+’ and ‘^’ in a different order than they appear in ASCII table or the way SQL sorts them.
In ASCII table ‘+’ has value of 42 and ‘^’ has value of 94 but if you run code like this:
var list = new List<string> { "+", "^", "!" };
list.Sort();
The list will contain values in the following order:
{ “!”, “^”, “+” }
LINQ sort generates the same result. Can someone tell me what kind of sort .NET does?
.NET doesn’t use ASCII, it uses Unicode. When you perform a string sort, .NET (by default) uses the current culture’s rules for sorting. In this case, those rules indicate that “^” comes before “+”. You can get the result you expect by using the “ordinal” string comparer: