I found very confusing when sorting a text file. Different algorithm/application produces different result, for example, on comparing two string str1=";P" and str2="-_-"
Just for your reference here gave the ASCII for each char in those string:
char(';') = 59; char('P') = 80;
char('-') = 45; char('_') = 95;
So I’ve tried different methods to determine which string is bigger, here is my result:
-
In Microsoft Office Excel Sorting command:
“;P” < “-_-“
-
C++ std::string::compare(string &str2), i.e.
str1.compare(str2)“;P” > “-_-“
-
C# string.CompareTo(), i.e.
str1.CompareTo(str2)“;P” < “-_-“
-
C# string.CompareOrdinal(), i.e.
CompareOrdinal(w1, w2)“;P” > “-_-“
As shown, the result varied! Actually my intuitive result should equal to Method 2 and 4, since the ASCII(‘;’) = 59 which is larger than ASCII(‘-‘) = 45 .
So I have no idea why Excel and C# string.CompareTo() gives a opposite answer. Noted that in C# the second comparison function named string.CompareOrdinal(). Does this imply that the default C# string.CompareTo() function is not “Ordinal” ?
Could anyone explain this inconsistency?
And could anyone explain in CultureInfo = {en-US}, why it tells ;P > -_- ? what’s the underlying motivation or principle? And I have ever heard about different double multiplication in different cultureInfo. It’s rather a cultural shock..!
std::string::compare: “the result of a character comparison depends only on its character code”. It’s simply ordinal.String.CompareTo: “performs a word (case-sensitive and culture-sensitive) comparison using the current culture”. So,this not ordinal, since typical users don’t expect things to be sorted like that.String::CompareOrdinal: Per the name, “performs a case-sensitive comparison using ordinal sort rules”.EDIT:
CompareOptionshas a hint: “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.”