Comparer<T>.IComparer.Compare(Object, Object) Method

Definition

比较两个对象并返回一个值,该值指示一个对象小于、等于还是大于另一个对象。Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

 virtual int System.Collections.IComparer.Compare(System::Object ^ x, System::Object ^ y) = System::Collections::IComparer::Compare;
int IComparer.Compare (object x, object y);
abstract member System.Collections.IComparer.Compare : obj * obj -> int
override this.System.Collections.IComparer.Compare : obj * obj -> int
Function Compare (x As Object, y As Object) As Integer Implements IComparer.Compare

Parameters

x
Object

要比较的第一个对象。The first object to compare.

y
Object

要比较的第二个对象。The second object to compare.

Returns

Int32

一个有符号整数,指示 xy 的相对值,如下表所示。A signed integer that indicates the relative values of x and y, as shown in the following table.

Value 含义Meaning
小于零Less than zero x 小于 yx is less than y.
Zero x 等于 yx equals y.
大于零Greater than zero x 大于 yx is greater than y.

Implements

Exceptions

xy 所属的类型无法被强制转换为类型 Tx or y is of a type that cannot be cast to type T.

-或--or- xy 不实现 IComparable<T> 泛型接口或 IComparable 接口。x and y do not implement either the IComparable<T> generic interface or the IComparable interface.

Examples

下面的示例演示如何使用 IComparer.Compare 方法比较两个对象。The following example shows how to use the IComparer.Compare method to compare two objects. 此示例是为 Comparer<T> 类提供的更大示例的一部分。This example is part of a larger example provided for the Comparer<T> class.


// This explicit interface implementation
// compares first by the length.
// Returns -1 because the length of BoxA
// is less than the length of BoxB.
BoxLengthFirst LengthFirst = new BoxLengthFirst();

Comparer<Box> bc = (Comparer<Box>) LengthFirst;

Box BoxA = new Box(2, 6, 8);
Box BoxB = new Box(10, 12, 14);
int x = LengthFirst.Compare(BoxA, BoxB);
Console.WriteLine();
Console.WriteLine(x.ToString());

' This explicit interface implementation
' compares first by the length.
' Returns -1 because the length of BoxA
' is less than the length of BoxB.
Dim LengthFirst As New BoxLengthFirst()

Dim bc As Comparer(Of Box) = CType(LengthFirst, Comparer(Of Box))

Dim BoxA As New Box(2, 6, 8)
Dim BoxB As New Box(10, 12, 14)
Dim x As Integer = LengthFirst.Compare(BoxA, BoxB)
Console.WriteLine()
Console.WriteLine(x.ToString())

Remarks

此方法是 Compare(T, T) 方法的包装,因此 obj 必须强制转换为当前实例的泛型参数 T 指定的类型。This method is a wrapper for the Compare(T, T) method, so obj must be cast to the type specified by the generic argument T of the current instance. 如果无法将其强制转换为 T,则会引发 ArgumentExceptionIf it cannot be cast to T, an ArgumentException is thrown.

允许比较 null 与任何引用类型,并且不会生成异常。Comparing null with any reference type is allowed and does not generate an exception. 进行排序时,null 被视为小于任何其他对象。When sorting, null is considered to be less than any other object.

Notes to Callers

Compare(T, T)System.Collections.Generic.EqualityComparer`1.Equals(\`0,\`0) 在区分区域性和区分大小写方面具有不同的行为。 Compare(T, T) and System.Collections.Generic.EqualityComparer`1.Equals(\`0,\`0) behave differently in terms of culture-sensitivity and case-sensitivity. 对于字符串比较,建议将 StringComparer<string>上的比较器For string comparisons, the StringComparer class is recommended over Comparer<String>. StringComparer 类的属性返回预定义实例,这些实例可执行具有不同的区域性敏感性和区分大小写的组合的字符串比较。Properties of the StringComparer class return predefined instances that perform string comparisons with different combinations of culture-sensitivity and case-sensitivity. 同一个 StringComparer 实例的成员之间的区分大小写和区分大小写。The case-sensitivity and culture-sensitivity are consistent among the members of the same StringComparer instance. 有关区域性特定比较的详细信息,请参阅 System.Globalization 命名空间以及[全球化和本地化](/dotnet/standard/globalization-localization/)。For more information on culture-specific comparisons, see the System.Globalization namespace and [Globalization and Localization](/dotnet/standard/globalization-localization/).

Applies to

See also