Comparer<T>.IComparer.Compare(Object, Object) 方法

定义

比较两个对象并返回一个值,该值指示一个对象小于、等于还是大于另一个对象。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

参数

x
Object

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

y
Object

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

返回

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.

实现

例外

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.

示例

下面的示例演示如何使用 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())

注解

此方法是方法的包装 Compare(T, T) ,因此 obj 必须强制转换为当前实例的泛型参数所指定的类型 TThis 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 ,则 ArgumentException 会引发。If 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.

调用方说明

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 建议使用类,而不是比较器 < 字符串 > 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. 在同一实例的成员之间区分大小写和区分区域性 StringComparerThe case-sensitivity and culture-sensitivity are consistent among the members of the same StringComparer instance.

有关区域性特定比较的详细信息,请参阅 System.Globalization 命名空间和 全球化和本地化For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

适用于

另请参阅