It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int, long,
uint, float, double, string, or DateTime types, depending on whether x is
less than, equal to, or greater than y. But when x and y are sbyte, short,
byte, or ushort types, the return value is y-x expressed as a signed integer.
Although this behavior conforms to the definition Of IComparable, it seems
inconsistent. Is there a reason for this?
The code I used to test this is like the following:
short short1 = 5;
short short2 = 10;
Console.WriteLine ("For short, {0} compared to {1} is {2}.", short1, short2,
short1.CompareTo(short2));
int int1 = 5;
int int2 = 10;
Console.WriteLine ("For int, {0} compared to {1} is {2}.", int1, int2,
int1.CompareTo(int2));
Jon Skeet [C# MVP] - 10 Jun 2005 22:18 GMT
> It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int, long,
> uint, float, double, string, or DateTime types, depending on whether x is
[quoted text clipped - 3 lines]
> Although this behavior conforms to the definition Of IComparable, it seems
> inconsistent. Is there a reason for this?
I suspect it's the easiest way of computing it. Note that you couldn't
compute it that way for the other types, as you could end up wrapping
round (eg x=-very big number, y=+very big number, x-y=+big number).
This can't happen with the shorter types, due to the range of int
easily coping with subtracting the largest positive short (etc) from
the largest negative one.
Given that having done the subtraction the result is already there, why
bother to convert it to -1/0/1?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
howard39@nospam.nospam - 10 Jun 2005 22:35 GMT
That seems like an implementation-dependent choice. Anyhow, is it clear that
one subtract is faster than one or two compares?
> > It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int, long,
> > uint, float, double, string, or DateTime types, depending on whether x is
[quoted text clipped - 13 lines]
> Given that having done the subtraction the result is already there, why
> bother to convert it to -1/0/1?
Jon Skeet [C# MVP] - 11 Jun 2005 07:50 GMT
> That seems like an implementation-dependent choice.
Absolutely - which is entirely consistent with the interface.
> Anyhow, is it clear that one subtract is faster than one or two compares?
I don't know, but I would guess they've tried it.
I'm afraid I really don't see the problem though. It correctly
implements the interface, so unless someone starts relying on behaviour
which isn't specified in the interface (in which case they're at fault)
there should be no problem.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
cody - 20 Jun 2005 20:40 GMT
You should use Math.Sign(short1.CompareTo(short2)) which will return only
1,0 or -1.
> It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int,
> long,
[quoted text clipped - 18 lines]
> Console.WriteLine ("For int, {0} compared to {1} is {2}.", int1, int2,
> int1.CompareTo(int2));