StringComparer.Compare Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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. |
- Source:
- StringComparer.cs
- Source:
- StringComparer.cs
- Source:
- StringComparer.cs
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
Parameters
- x
- Object
An object to compare to y
.
- y
- Object
An object to compare to x
.
Returns
A signed integer that indicates the relative values of x
and y
, as shown in the following table.
Value | Meaning |
---|---|
Less than zero | x precedes y in the sort order, or x is null and y is not null .
|
Zero | x is equal to y , or x and y are both null .
|
Greater than zero | x follows y in the sort order, or y is null and x is not null .
|
Implements
Exceptions
Neither x
nor y
is a String object, and neither x
nor y
implements the IComparable interface.
Remarks
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.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- StringComparer.cs
- Source:
- StringComparer.cs
- Source:
- StringComparer.cs
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
Parameters
- x
- String
A string to compare to y
.
- y
- String
A string to compare to x
.
Returns
A signed integer that indicates the relative values of x
and y
, as shown in the following table.
Value | Meaning |
---|---|
Less than zero | x precedes y in the sort order, or x is null and y is not null .
|
Zero | x is equal to y , or x and y are both null .
|
Greater than zero | x follows y in the sort order, or y is null and x is not null .
|
Implements
Remarks
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.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: