Kevin Doyon
Kevin Doyon

Reputation: 3578

How does String.CompareTo work?

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"...

Upvotes: 3

Views: 2687

Answers (1)

Bryan Menard
Bryan Menard

Reputation: 13364

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.

Upvotes: 8

Related Questions