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

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