IComparable.CompareTo Method
Compares the current instance with another object of the same type.
[Visual Basic] Function CompareTo( _ ByVal obj As Object _ ) As Integer [C#] int CompareTo( object obj ); [C++] int CompareTo( Object* obj ); [JScript] function CompareTo( obj : Object ) : int;
Parameters
- obj
- An object to compare with this instance.
Return Value
A 32-bit signed integer that indicates the relative order of the comparands. The return value has these meanings:
| Value | Meaning |
|---|---|
| Less than zero | This instance is less than obj. |
| Zero | This instance is equal to obj. |
| Greater than zero | This instance is greater than obj. |
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentException | obj is not the same type as this instance. |
Remarks
This method is only a definition and must be implemented by a specific class or value type to have effect. The meaning of the comparisons, "less than," "equal to," and "greater than," depends on the particular implementation.
By definition, any object compares greater than a null reference (Nothing in Visual Basic); and two null references compare equal to each other.
The parameter, obj, must be the same type as the class or value type that implements this interface; otherwise, an ArgumentException is thrown.
Notes to Implementers: For any objects A, B and C, the following must be true:
A.CompareTo(A) is required to return zero.
If A.CompareTo(B) returns zero then B.CompareTo(A) is required to return zero.
If A.CompareTo(B) returns zero and B.CompareTo(C) returns zero then A.CompareTo(C) is required to return zero.
If A.CompareTo(B) returns a value other than zero then B.CompareTo(A) is required to return a value of the opposite sign.
If A.CompareTo(B) returns a value x not equal to zero, and B.CompareTo(C) returns a value y of the same sign as x, then A.CompareTo(C) is required to return a value of the same sign as x and y.
Notes to Callers: Use the CompareTo method to determine the ordering of instances of a class.
Example
[Visual Basic, C#, C++] The following code sample illustrates the use of CompareTo to compare a Temperature object implementing IComparable with another object.
[Visual Basic] Public Class Temperature Implements IComparable Public Overloads Function CompareTo(ByVal obj As Object) As Integer _ Implements IComparable.CompareTo If TypeOf obj Is Temperature Then Dim temp As Temperature = CType(obj, Temperature) Return m_value.CompareTo(temp.m_value) End If Throw New ArgumentException("object is not a Temperature") End Function ' The value holder Protected m_value As Integer Public Property Value() As Integer Get Return m_value End Get Set(ByVal Value As Integer) m_value = Value End Set End Property Public Property Celsius() As Integer Get Return (m_value - 32) / 2 End Get Set(ByVal Value As Integer) m_value = Value * 2 + 32 End Set End Property End Class [C#] public class Temperature : IComparable { /// <summary> /// IComparable.CompareTo implementation. /// </summary> public int CompareTo(object obj) { if(obj is Temperature) { Temperature temp = (Temperature) obj; return m_value.CompareTo(temp.m_value); } throw new ArgumentException("object is not a Temperature"); } // The value holder protected int m_value; public int Value { get { return m_value; } set { m_value = value; } } public int Celsius { get { return (m_value-32)/2; } set { m_value = value*2+32; } } } [C++] public __gc class Temperature : public IComparable { /// <summary> /// IComparable.CompareTo implementation. /// </summary> protected: // The value holder Double m_value; public: Int32 CompareTo(Object* obj) { if(obj->GetType() == __typeof(Temperature)) { Temperature* temp = dynamic_cast<Temperature*>(obj); return m_value.CompareTo(__box(temp->m_value)); } throw new ArgumentException("object is not a Temperature"); } __property Double get_Value() { return m_value; } __property void set_Value(Double value) { m_value = value; } __property Double get_Celsius() { return (m_value-32)/1.8; } __property void set_Celsius(Double value) { m_value = value*1.8 + 32; } };
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
IComparable Interface | IComparable Members | System Namespace | Object