System.Collections.IComparer Interface

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Exposes a method that compares two objects.
C# Syntax:
public interface IComparer
Remarks
This interface is used in conjunction with the Array.Sort and Array.BinarySearch methods. It provides a way to customize the sort order of a collection.

The default implementation of this interface is the Comparer class.

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 the conceptual topic at MSDN: designingglobalapplications.

See also:
System.Collections Namespace | Comparer | Thread.CurrentCulture | CompareInfo | CultureInfo

System.Collections.IComparer Member List:

Public Methods
Compare Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other.

System.Collections.IComparer Member Details

Method: Compare(
   object x,
   object y
)
Summary
Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other.
C# Syntax:
int Compare(
   object x,
   object y
);
Parameters:

x

First object to compare.

y

Second object to compare.

Return Value:


Value Condition
Less than zero is less than .
Zero equals .
Greater than zero is greater than .
Exceptions
Exception Type Condition
ArgumentException Neither x nor y implements the IComparable interface.

-or-

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

Remarks
The preferred implementation is to use the IComparable.CompareTo method of one of the parameters.

Comparing null with any type is allowed and does not generate an exception when using IComparable. When sorting, null 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 the conceptual topic at MSDN: designingglobalapplications.

See also:
IComparable | Thread.CurrentCulture | CompareInfo | CultureInfo

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.