tags:

views:

655

answers:

1

When I compare strings containing positive/negative numbers, for example:

int res1 = "-1".CompareTo("1");
int res2 = "-1".CompareTo("2");

res1 equals 1.
res2 equals -1.

How does String.CompareTo work?? That would mean it is order is "2 -1 1"...

+8  A: 

From MSDN:

Certain nonalphanumeric characters might have special weights assigned to them. 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.

Edit: Forgot to mention, this is related to the CompareOptions enumeration used by string.Compare.

Bryan Menard
This is the best answer so far, since it addresses the the actual oddity, instead of assuming something and claiming the behaviour makes sense from that assumption.
Sean Nyman