Export (0) Print
Expand All

IComparer.Compare Method

Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other.

[Visual Basic]
Function Compare( _
   ByVal x As Object, _
   ByVal y As Object _
) As Integer
[C#]
int Compare(
   object x,
 object y
);
[C++]
int Compare(
   Object* x,
 Object* y
);
[JScript]
function Compare(
   x : Object,
 y : Object
) : int;

Parameters

x
First object to compare.
y
Second object to compare.

Return Value

Value Condition
Less than zero x is less than y.
Zero x equals y.
Greater than zero x is greater than y.

Exceptions

Exception Type Condition
ArgumentException Neither x nor y implements the IComparable interface.

-or-

x and y are of different types and neither one can handle comparisons with the other.

Remarks

The preferred implementation is to use the CompareTo method of one of the parameters.

Comparing a null reference (Nothing in Visual Basic) with any type is allowed and does not generate an exception when using IComparable. When sorting, a null reference (Nothing) is considered to be less than any other object.

Comparison procedures use the Thread.CurrentCulture of the current thread unless otherwise specified. String comparisons might have different results depending on the culture. For more information on culture-specific comparisons, see the System.Globalization namespace and Developing World-Ready Applications.

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

IComparer Interface | IComparer Members | System.Collections Namespace | IComparable | Thread.CurrentCulture | System.Globalization.CompareInfo | System.Globalization.CultureInfo

Show:
© 2014 Microsoft