System.Collections.Generic.IComparer<T> Interface

public interface IComparer<T>

Assembly

mscorlib

Library

BCL

Summary

Provides a mechanism to customize comparison in sort ordering of a generic collection.

See Also

System.Collections.Generic Namespace

Members

IComparer<T> Methods

IComparer<T>.Compare Method


IComparer<T>.Compare Method

int Compare(T x, T y)

Summary

Returns the sort order of two T instances.

Parameters

x
First T to compare.
y
Second T to compare.

Return Value

A Int32 containing a value that reflects the sort order of x as compared to y. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number.

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:

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

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

If Compare(A,B) is zero, then Compare(B,C) and Compare(A,C) must have the same sign (negative, zero or positive).

If Compare(B,C) is zero, then Compare(A,B) and Compare(A,C) must have the same sign (negative, zero or positive).

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

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

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

The exact behavior of this method is unspecified. The intent of this 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.

]

See Also

System.Collections.Generic.IComparer<T> Interface, System.Collections.Generic Namespace