Question

if I type this into my Immediate Window

String.Compare("AA", "SA");

I get a result of 1

surely this is wrong? AA is less than SA so shouldn't it be -1?

I am running .NET 4

Was it helpful?

Solution

In the Danish culture "AA" is treated as a single letter "Å" and alphabetically it comes after "Z".

CultureInfo cultureInfo = CultureInfo.GetCultureInfo("da-DK");
int comparision = String.Compare("AA", "SA", false, cultureInfo);
Console.WriteLine(comparision);

Result:

1

To get the result you want you can use invariant culture (or a specific culture that has the sort order that you desire):

CultureInfo cultureInfo = CultureInfo.InvariantCulture;
int comparision = String.Compare("AA", "SA", false, cultureInfo);
Console.WriteLine(comparision);

Result:

-1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top