I’m hoping someone can point out something dumb I’m doing here, but I’ve simplified this as much as I can, and I still get (what I believe) to be an inconsistent result. Upper case letters precede lower case in the ASCII sequence, but the .CompareTo() method returns 1 when I compare ‘A’ to ‘a’, implying it thinks ‘A’ comes after ‘a’ in the sort sequence. It works for other lower case letters.
Could someone try the code and let me know what you get?
Here’s the code (.Net 8 — same results in both Visual Studio 2022 and Visual Studio Code with C# extensions):
int result;
string str1 = “A”;
string str2 = “a”;
string str3 = “b”;
string str4 = “z”;
Console.WriteLine(“=============”);
result = str1.CompareTo(str2);
Console.WriteLine(“A to a: ” + result); // Output: -1 (since ‘A’ < ‘a’)
result = str1.CompareTo(str3);
Console.WriteLine(“A to b: ” + result); // Output: -1 (since ‘A’ < ‘b’)
result = str1.CompareTo(str4);
Console.WriteLine(“A to z: ” + result); // Output: -1 (since ‘A’ < ‘z’)
And here is the output I’m getting:
=============
A to a: 1 // this should be a -1, right???
A to b: -1
A to z: -1
Thanks in advance!!