문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top