Mono Class Library: System.Collections.IComparer Overview | Members

System.Collections.IComparer.Compare Method

Returns the sort order of two object instances. [Edit]

public int Compare (object x, object y)

Parameters

x
First object to compare. [Edit]
y
Second object to compare. [Edit]

Returns

The return value is a negative number, zero, or a positive number reflecting the sort order of x as compared to y. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

ValueCondition
A negative numberx < y.
Zerox == y.
A positive numberx > y.
[Edit]

Remarks

Operation

For any objects A, B, and C, the following are required to be true:

IComparer.Compare(object, object) (A, A) is required to return zero.

If IComparer.Compare(object, object)(A, B) returns zero, then IComparer.Compare(object, object) (B, A) is required to return zero.

If IComparer.Compare(object, object)(A, B) returns zero and IComparer.Compare(object, object)(B, C) returns zero then IComparer.Compare(object, object) (A, C) is required to return zero.

If IComparer.Compare(object, object)(A, B) returns a value other than zero, then IComparer.Compare(object, object) (B, A) is required to return a value of the opposite sign.

If IComparer.Compare(object, object)(A, B) returns a value x not equal to zero, and IComparer.Compare(object, object)(B, C) returns a value y of the same sign as x, then IComparer.Compare(object, object) (A, C) is required to return a value of the same sign as x and y.

Note:

The exact ordering of this method is unspecified. The intent of the method is to provide a mechanism that orders instances of a class in a manner that is consistent with the mathematical definitions of the relational operators (<, >, and ==), without regard for class-specific definitions of the operators.

Usage

This interface is used in conjunction with the Array.Sort(Array) and Array.BinarySearch(Array, object) methods.

[Edit]

Requirements

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0