public interface IComparer
mscorlib
BCL
Provides a mechanism to customize the sort ordering of a collection.
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.]
System.Collections Namespace
IComparer Methods
int Compare(object x, object y);
Returns the sort order of two Object instances.
- x
- First Object to compare.
- y
- Second Object to compare.
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:
Value Condition A negative number x < y. Zero x == y. A positive number x > y.
[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.
]
System.Collections.IComparer Interface, System.Collections Namespace