CompareOptions enum

This article provides supplementary remarks to the reference documentation for this API.

The CompareOptions options denote case sensitivity or necessity to ignore types of characters.

.NET uses three distinct ways of sorting: word sort, string sort, and ordinal sort. Word sort performs a culture-sensitive comparison of strings. Certain nonalphanumeric characters might have special weights assigned to them. For example, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. String sort is similar to word sort, except that there are no special cases. Therefore, all nonalphanumeric symbols come before all alphanumeric characters. Ordinal sort compares strings based on the Unicode values of each element of the string. For a downloadable set of text files that contain information on the character weights used in sorting and comparison operations for Windows operating systems, see Sorting Weight Tables. For the sort weight table for Linux and macOS, see the Default Unicode Collation Element Table. The specific version of the sort weight table on Linux and macOS depends on the version of the International Components for Unicode libraries installed on the system. For information on ICU versions and the Unicode versions that they implement, see Downloading ICU.

The StringSort value can only be used with CompareInfo.Compare and CompareInfo.GetSortKey. ArgumentException is thrown if the StringSort value is used with CompareInfo.IsPrefix, CompareInfo.IsSuffix, CompareInfo.IndexOf, or CompareInfo.LastIndexOf.

Note

When possible, you should use string comparison methods that accept a CompareOptions value to specify the kind of comparison expected. As a general rule, user-facing comparisons are best served by the use of linguistic options (using the current culture), while security comparisons should specify Ordinal or OrdinalIgnoreCase.

Culture-sensitive sorts

Note

.NET Core running on Linux and macOS systems only: The collation behavior for the C and Posix cultures is always case-sensitive because these cultures do not use the expected Unicode collation order. We recommend that you use a culture other than C or Posix for performing culture-sensitive, case-insensitive sorting operations.