CompareOptions Enumeration
Defines the string comparison options to use with CompareInfo.
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.GlobalizationAssembly: mscorlib (in mscorlib.dll)
| Member name | Description | |
|---|---|---|
![]() ![]() | None | Indicates the default option settings for string comparisons. |
![]() ![]() | IgnoreCase | Indicates that the string comparison must ignore case. |
![]() ![]() | IgnoreNonSpace | Indicates that the string comparison must ignore nonspacing combining characters, such as diacritics. The Unicode Standard defines combining characters as characters that are combined with base characters to produce a new character. Nonspacing combining characters do not occupy a spacing position by themselves when rendered. |
![]() ![]() | IgnoreSymbols | Indicates that the string comparison must ignore symbols, such as white-space characters, punctuation, currency symbols, the percent sign, mathematical symbols, the ampersand, and so on. |
![]() ![]() | IgnoreKanaType | Indicates that the string comparison must ignore the Kana type. Kana type refers to Japanese hiragana and katakana characters, which represent phonetic sounds in the Japanese language. Hiragana is used for native Japanese expressions and words, while katakana is used for words borrowed from other languages, such as "computer" or "Internet". A phonetic sound can be expressed in both hiragana and katakana. If this value is selected, the hiragana character for one sound is considered equal to the katakana character for the same sound. |
![]() ![]() | IgnoreWidth | Indicates that the string comparison must ignore the character width. For example, Japanese katakana characters can be written as full-width or half-width. If this value is selected, the katakana characters written as full-width are considered equal to the same characters written as half-width. |
![]() ![]() | OrdinalIgnoreCase | String comparison must ignore case, then perform an ordinal comparison. This technique is equivalent to converting the string to uppercase using the invariant culture and then performing an ordinal comparison on the result. |
![]() ![]() | StringSort | Indicates that the string comparison must use the string sort algorithm. In a string sort, the hyphen and the apostrophe, as well as other nonalphanumeric symbols, come before alphanumeric characters. |
![]() ![]() | Ordinal | Indicates that the string comparison must use successive Unicode UTF-16 encoded values of the string (code unit by code unit comparison), leading to a fast comparison but one that is culture-insensitive. A string starting with a code unit XXXX16 comes before a string starting with YYYY16, if XXXX16 is less than YYYY16. This value cannot be combined with other CompareOptions values and must be used alone. |
These options denote case sensitivity or necessity to ignore types of characters.
The .NET Framework 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.
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, the application 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. |
The following code example shows how sorting with StringSort differs from sorting without StringSort.
using namespace System; using namespace System::Collections; using namespace System::Globalization; // __gc public class SamplesCompareOptions { ref class MyStringComparer: public IComparer { public: // Constructs a comparer using the specified CompareOptions. CompareInfo^ myComp; CompareOptions myOptions; MyStringComparer( CompareInfo^ cmpi, CompareOptions options ) : myComp( cmpi ), myOptions( options ) {} // Compares strings with the CompareOptions specified in the constructor. virtual int Compare( Object^ a, Object^ b ) { if ( a == b ) return 0; if ( a == nullptr ) return -1; if ( b == nullptr ) return 1; String^ sa = dynamic_cast<String^>(a); String^ sb = dynamic_cast<String^>(b); if ( sa != nullptr && sb != nullptr ) return myComp->Compare( sa, sb, myOptions ); throw gcnew ArgumentException( "a and b should be strings." ); } }; int main() { // Creates and initializes an array of strings to sort. array<String^>^myArr = {"cant","bill's","coop","cannot","billet","can't","con","bills","co-op"}; Console::WriteLine( "\nInitially, " ); IEnumerator^ myEnum = myArr->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ myStr = safe_cast<String^>(myEnum->Current); Console::WriteLine( myStr ); } // Creates and initializes a Comparer to use. //CultureInfo* myCI = new CultureInfo(S"en-US", false); MyStringComparer^ myComp = gcnew MyStringComparer( CompareInfo::GetCompareInfo( "en-US" ),CompareOptions::None ); // Sorts the array without StringSort. Array::Sort( myArr, myComp ); Console::WriteLine( "\nAfter sorting without CompareOptions::StringSort:" ); myEnum = myArr->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ myStr = safe_cast<String^>(myEnum->Current); Console::WriteLine( myStr ); } // Sorts the array with StringSort. myComp = gcnew MyStringComparer( CompareInfo::GetCompareInfo( "en-US" ),CompareOptions::StringSort ); Array::Sort( myArr, myComp ); Console::WriteLine( "\nAfter sorting with CompareOptions::StringSort:" ); myEnum = myArr->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ myStr = safe_cast<String^>(myEnum->Current); Console::WriteLine( myStr ); } } /* This code produces the following output. Initially, cant bill's coop cannot billet can't con bills co-op After sorting without CompareOptions::StringSort: billet bills bill's cannot cant can't con coop co-op After sorting with CompareOptions::StringSort: bill's billet bills can't cannot cant co-op con coop */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.


Note