StringComparer.Compare 方法

定义

比较两个对象或字符串,并返回其相对排序顺序的指示。Compares two objects or strings and returns an indication of their relative sort order.

重载

Compare(Object, Object)

当在派生类中重写时,将比较两个对象并返回其相对排序顺序的指示。When overridden in a derived class, compares two objects and returns an indication of their relative sort order.

Compare(String, String)

当在派生类中重写时,将比较两个字符串并返回其相对排序顺序的指示。When overridden in a derived class, compares two strings and returns an indication of their relative sort order.

Compare(Object, Object)

当在派生类中重写时,将比较两个对象并返回其相对排序顺序的指示。When overridden in a derived class, compares two objects and returns an indication of their relative sort order.

public:
 virtual int Compare(System::Object ^ x, System::Object ^ y);
public int Compare (object? x, object? y);
public int Compare (object x, object y);
abstract member Compare : obj * obj -> int
override this.Compare : obj * obj -> int
Public Function Compare (x As Object, y As Object) As Integer

参数

x
Object

要与 y 比较的对象。An object to compare to y.

y
Object

要与 x 比较的对象。An object to compare to x.

返回

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 在排序顺序中位于 y 之前,或者 xnully 不为 nullx precedes y in the sort order, or x is null and y is not null.
Zero x 等于 y,或者 xy 都为 nullx is equal to y, or x and y are both null.
大于零Greater than zero x 在排序顺序中位于 y 之后,或者 ynullx 不为 nullx follows y in the sort order, or y is null and x is not null.

实现

例外

xy 都不是 String 对象,并且 xy 都不实现 IComparable 接口。Neither x nor y is a String object, and neither x nor y implements the IComparable interface.

注解

StringComparer.Compare(String, String)方法比方法稍微高效, StringComparer.Compare(Object, Object) 因为 x y 执行比较不需要转换和自变量。The StringComparer.Compare(String, String) method is slightly more efficient than the StringComparer.Compare(Object, Object) method because no conversion of the x and y arguments is needed to perform the comparison.

适用于

Compare(String, String)

当在派生类中重写时,将比较两个字符串并返回其相对排序顺序的指示。When overridden in a derived class, compares two strings and returns an indication of their relative sort order.

public:
 abstract int Compare(System::String ^ x, System::String ^ y);
public abstract int Compare (string x, string y);
public abstract int Compare (string? x, string? y);
abstract member Compare : string * string -> int
Public MustOverride Function Compare (x As String, y As String) As Integer

参数

x
String

要与 y 比较的字符串。A string to compare to y.

y
String

要与 x 比较的字符串。A string to compare to x.

返回

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 在排序顺序中位于 y 之前,或者 xnully 不为 nullx precedes y in the sort order, or x is null and y is not null.
Zero x 等于 y,或者 xy 都为 nullx is equal to y, or x and y are both null.
大于零Greater than zero x 在排序顺序中位于 y 之后,或者 ynullx 不为 nullx follows y in the sort order, or y is null and x is not null.

实现

注解

StringComparer.Compare(String, String)方法比方法稍微高效, StringComparer.Compare(Object, Object) 因为 x y 执行比较不需要转换和自变量。The StringComparer.Compare(String, String) method is slightly more efficient than the StringComparer.Compare(Object, Object) method because no conversion of the x and y arguments is needed to perform the comparison.

适用于