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- xnully 不是 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- xnully 不是 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.

適用於