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?
There isn’t necessarily a consistent character-by-character ordering for any particular locale.
From the MSDN documentation:
The only way to ensure consistent character-by-character ordering is by using an ordinal comparison, as demonstrated in Anton’s answer.