System.Collections.Comparer.Compare Method

Performs a case-sensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

Syntax

public int Compare (object a, object b)

Parameters

a
The first object to compare.
a
The first object to compare.
b
The second object to compare.

Returns

A signed integer that indicates the relative values of a and b, as shown in the following table.

Less than zero

a is less than b.

Zero

a equals b.

Greater than zero

a is greater than b.

Exceptions

TypeReason
ArgumentException

Both a and b are not null and do not implement the IComparable interface.

-or-

Both a and b are not null and are not assignment-compatible types.

Remarks

If a implements IComparable, then a. CompareTo (b) is returned; otherwise, if b implements IComparable, then the negated result of b. CompareTo (a) is returned.

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.

String comparisons might have different results depending on the culture. For more information on culture-specific comparisons, see the System.Globalization namespace and Encoding and Localization.

Requirements

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