14

I always assumed that .Net compares strings lexicographically, according to the current culture. But there is something strange when one of the strings ends on '-':

"+".CompareTo("-")
Returns: 1

"+1".CompareTo("-1")
Returns: -1

I get it an all cultures I tried, including the invariant one. Can anyone explain what is going on, and how can I get the consistent character-by-character ordering for the current locale?

1
  • Same for "x+".CompareTo("x-") and "x+1".CompareTo("x-1")
    – H H
    Feb 11, 2010 at 13:15

5 Answers 5

10

Try changing this to

string.Compare("+", "-", StringComparison.Ordinal); // == -2
string.Compare("+1", "-1", StringComparison.Ordinal); // == -2
3
  • 1
    Thank you Anton, but Ordinal means the old good ASCII sort, with all uppercase going before lowercase. string.Compare("a", "Z", StringComparison.Ordinal); 7 In my locale, the case-sensitive ordering is something like 'a'<'A'<'ä'<'Ä'<...<'z'<'Z', but I see now way to access it directly.
    – jmster
    Feb 11, 2010 at 13:20
  • jmster: Why not write your own sort? Are you just concerned about numbers?
    – Noon Silk
    Feb 11, 2010 at 13:23
  • I have written my own method, but in the bottom I need some way to compare the characters according to the local rules. And I do not want to code the locales by hand, it should be possible to get it from Windows
    – jmster
    Feb 11, 2010 at 13:28
7

There isn't necessarily a consistent character-by-character ordering for any particular locale.

From the MSDN documentation:

For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

The only way to ensure consistent character-by-character ordering is by using an ordinal comparison, as demonstrated in Anton's answer.

4
  • This explains the matter. The only thing I wonder is why on earth they need to introduce such magic treatment for all-purpose characters like ASCII minus?
    – jmster
    Feb 11, 2010 at 13:47
  • @jmster: My guess is that the + and - characters don't really mean anything in isolation, so in that situation an ordinal comparison takes place where + (code 43) evaluates to "less than" - (code 45). However, when the + and - characters prefix a number then they take on a meaning, and in that situation a "semantic" and/or numeric comparison takes place where +1 is greater than -1.
    – LukeH
    Feb 11, 2010 at 14:02
  • Actually it's the quite opposite, .NET uses ASCII ordering to conclude that "+1" is less than "-1", but in the final position minus magically turns into hyphen, so that "-" is sorted somewhere between "\a" (bell) and " " (blank space).
    – jmster
    Feb 11, 2010 at 18:25
  • @jmster: Yes, you're right. My guess was wrong. I think the excerpt from the documentation still applies, but I've no idea what particular logic applies to the different comparisons in this case.
    – LukeH
    Feb 11, 2010 at 22:45
3
        string.Compare("+", "-");
        string.Compare("+", "-", StringComparison.CurrentCulture);
        string.Compare("+", "-", StringComparison.InvariantCulture);
        string.Compare("+", "-", StringComparison.InvariantCultureIgnoreCase);

        // All Pass

the two value are equal because, inguisitic casing is being taken into consideration

FIX:

replace the invariant comparison with an ordinal comparison.This means the decisions are based on simple byte comparisons and ignore casing or equivalence tables that are parameterized by culture.

reference : Use ordinal StringComparison

string.Compare("+", "-", StringComparison.Ordinal); // fail

2

You'll probably want to use the true minus sign, Unicode codepoint \u2212. The minus sign you use in programming (\u002d) is a "hyphen-minus", its collation order is context sensitive because it is also frequently used as a hyphen. There's more than you'll want to know about the many different kind of dashes in this article.

2
  • 1
    Your observation is absolutely correct, the Unicode people seem to know better how each character has to be used. It's users' fault that they don't remember Unicode numbers and use the characters they see on the keyboard.
    – jmster
    Feb 11, 2010 at 18:38
  • Typography and programming languages are still a world apart. Maybe that will change some day, it will take a while. There are plenty of Alt+keypad shortcuts available to enter these kind of Unicode codepoints, ironically not for the minus sign. Feb 11, 2010 at 22:54
0

use CompareOrdinal. e.g

String.CompareOrdinal("+1","-1");
-2
String.CompareOrdinal("+","-");
-2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.