Edit

Share via


StringComparer.Compare Method

Definition

Compares two objects or strings and returns an indication of their relative sort order.

Overloads

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)

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 int Compare (object? x, object? y);
public int Compare (object x, object y);

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

Compare(String, String)

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 (string x, string y);
public abstract int Compare (string? x, string? y);

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