CompareOptions Enum

Definition

Defines the string comparison options to use with CompareInfo.

This enumeration supports a bitwise combination of its member values.

public enum class CompareOptions
[System.Flags]
public enum CompareOptions
[System.Flags]
[System.Serializable]
public enum CompareOptions
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CompareOptions
[<System.Flags>]
type CompareOptions = 
[<System.Flags>]
[<System.Serializable>]
type CompareOptions = 
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CompareOptions = 
Public Enum CompareOptions
Inheritance
CompareOptions
Attributes

Fields

IgnoreCase 1

Indicates that the string comparison must ignore case.

IgnoreKanaType 8

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.

IgnoreNonSpace 2

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 4

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.

IgnoreWidth 16

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.

None 0

Indicates the default option settings for string comparisons.

Ordinal 1073741824

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.

OrdinalIgnoreCase 268435456

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 536870912

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.

Examples

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
*/
using System;
using System.Collections;
using System.Globalization;

public class SamplesCompareOptions  {

   private class MyStringComparer: IComparer {
      private CompareInfo myComp;
      private CompareOptions myOptions = CompareOptions.None;

      // Constructs a comparer using the specified CompareOptions.
      public MyStringComparer( CompareInfo cmpi, CompareOptions options )  {
         myComp = cmpi;
         this.myOptions = options;
      }

      // Compares strings with the CompareOptions specified in the constructor.
      public int Compare(Object a, Object b) {
         if (a == b) return 0;
         if (a == null) return -1;
         if (b == null) return 1;

         String sa = a as String;
         String sb = b as String;
         if (sa != null && sb != null)
            return myComp.Compare(sa, sb, myOptions);
         throw new ArgumentException("a and b should be strings.");
      }
   }

   public static void Main()  {

      // Creates and initializes an array of strings to sort.
      String[] myArr = new String[9] { "cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op" };
      Console.WriteLine( "\nInitially," );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

      // Creates and initializes a Comparer to use.
      //CultureInfo myCI = new CultureInfo( "en-US", false );
      MyStringComparer myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None);

      // Sorts the array without StringSort.
      Array.Sort( myArr, myComp );
      Console.WriteLine( "\nAfter sorting without CompareOptions.StringSort:" );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

      // Sorts the array with StringSort.
      myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort);
      Array.Sort( myArr, myComp );
      Console.WriteLine( "\nAfter sorting with CompareOptions.StringSort:" );
      foreach ( String myStr in myArr )
         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

*/
Imports System.Collections
Imports System.Globalization

Public Class SamplesCompareOptions

   Private Class MyStringComparer
      Implements IComparer

      Private myComp As CompareInfo
      Private myOptions As CompareOptions = CompareOptions.None
      
      ' Constructs a comparer using the specified CompareOptions.
      Public Sub New(cmpi As CompareInfo, options As CompareOptions)
         myComp = cmpi
         Me.myOptions = options
      End Sub
      
      ' Compares strings with the CompareOptions specified in the constructor.
      Public Function Compare(a As [Object], b As [Object]) As Integer Implements IComparer.Compare
         If a = b Then
            Return 0
         End If
         If a Is Nothing Then
            Return - 1
         End If
         If b Is Nothing Then
            Return 1
         End If 

         Dim sa As [String] = a
         Dim sb As [String] = b
         If Not (sa Is Nothing) And Not (sb Is Nothing) Then
            Return myComp.Compare(sa, sb, myOptions)
         End If
         Throw New ArgumentException("a and b should be strings.")

      End Function 'Compare 

   End Class


   Public Shared Sub Main()
      
      ' Creates and initializes an array of strings to sort.
      Dim myArr() As [String] = {"cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op"}
      Console.WriteLine()
      Console.WriteLine("Initially,")
      Dim myStr As [String]
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

      ' Creates and initializes a Comparer to use.
      'CultureInfo myCI = new CultureInfo( "en-US", false );
      Dim myComp As New MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None)
      
      ' Sorts the array without StringSort.
      Array.Sort(myArr, myComp)
      Console.WriteLine()
      Console.WriteLine("After sorting without CompareOptions.StringSort:")
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

      ' Sorts the array with StringSort.
      myComp = New MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort)
      Array.Sort(myArr, myComp)
      Console.WriteLine()
      Console.WriteLine("After sorting with CompareOptions.StringSort:")
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

   End Sub

End Class


'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

Remarks

For more information about this API, see Supplemental API remarks for CompareOptions.

Applies to

See also