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);
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.

返回

一个有符号整数,指示 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 之前。x precedes y in the sort order. -or- xnull,且 y 不是 nullx is null and y is not null.
Zero x 等于 yx is equal to y. -or- xy 均为 nullx and y are both null.
大于零Greater than zero x 在排序顺序中位于 y 之后。x follows y in the sort order. -or- ynull,且 x 不是 nully 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.

注解

方法比方法稍微高效, x因为执行比较不需要转换和y自变量。 StringComparer.Compare(Object, Object) StringComparer.Compare(String, String)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);
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.

返回

一个有符号整数,指示 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 之前。x precedes y in the sort order. - 或 --or- xnull,且 y 不是 nullx is null and y is not null.
Zero x 等于 yx is equal to y. -or- xy 均为 nullx and y are both null.
大于零Greater than zero x 在排序顺序中位于 y 之后。x follows y in the sort order. -or- ynull,且 x 不是 nully is null and x is not null.

实现

注解

方法比方法稍微高效, x因为执行比较不需要转换和y自变量。 StringComparer.Compare(Object, Object) StringComparer.Compare(String, String)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.

适用于