This documentation is archived and is not being maintained.

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
int Compare(
   object x,
 object y
int Compare(
   Object* x,
 Object* y
function Compare(
   x : Object,
 y : Object
) : int;


First object to compare.
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.


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


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


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.


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