System.Collections.IComparer Interface

public interface IComparer

Assembly

mscorlib

Library

BCL

Summary

Provides a mechanism to customize the sort ordering of a collection.

Description

The default implementation of this interface is Comparer .

[Note: IComparer contains the System.Collections.IComparer.Compare(System.Object,System.Object) method. The consumer of an object should call this method when sorting members of a collection.]

See Also

System.Collections Namespace

Members

IComparer Methods

IComparer.Compare Method


IComparer.Compare Method

int Compare(object x, object y);

Summary

Returns the sort order of two Object instances.

Parameters

x
First Object to compare.
y
Second Object to compare.

Return Value

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.

Description

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

System.Collections.IComparer.Compare(System.Object,System.Object) (A, A) is required to return zero.

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

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

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

If System.Collections.IComparer.Compare(System.Object,System.Object)(A, B) returns a value x not equal to zero, and System.Collections.IComparer.Compare(System.Object,System.Object)(B, C) returns a value y of the same sign as x, then System.Collections.IComparer.Compare(System.Object,System.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 System.Array.Sort(System.Array) and System.Array.BinarySearch(System.Array,System.Object) methods.

]

See Also

System.Collections.IComparer Interface, System.Collections Namespace