views:

71

answers:

2

How this can be ? (This is taken from the immediate window in VS2008)

?string.Compare("-", "+")
-1
?string.Compare("-0", "+0")
1
+7  A: 

From the remarks on String.Compare (emphasis mine):

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. 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.

Joey
Hey Johannes, i saw that. But my machine is an XP English setup. And + and - are standard ascii characters below 127.I don't see how 0 can affect their relative order in my current culture.
Juan
While sometimes counter-intuitive, sophisticated string comparison functions such as `String.Compare` here can have niceties such as ordering "Strasse" near "Straße", "école" near "ecole" and "file9.txt" near "file10.txt".
Pascal Cuoq
@Juan the latter example "file9.txt" before "file10.txt" is desirable even in an English setup, and probably you are somehow falling in this case as soon as you add the zeroes.
Pascal Cuoq
Juan: Just because historically there was paid little special attention to collation in English doesn't mean that there *aren't* cases where an English locale might differ from pure ASCII or codepoint sort order. It's not like English is a language which doesn't need any special rules on computers.
Joey
Tank you all. The original problem is that i have to sort a list using a combinationof an int and a string field. To resolve that i've built a string that combines both trying to folow a string order.I've resolved that with an offset to the int value so all values are positive.As additional information c++ wcscmp behaves ok in my machine. Thank you again
Juan
C++ is not .NET. They follow different standards, so don't expect them to sort the same :-)
Joey
Yea, i know. Well, if i find the sorting rules somewhere or why this behaves this way, i'll post it here.Thank you all.
Juan
+1  A: 

The C# manual writes:

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. 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 comparison is performed using word sort rules. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

nes1983