Export (0) Print
Expand All

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.Globalization
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[FlagsAttribute] 
public enum CompareOptions
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute FlagsAttribute() */ 
public enum CompareOptions
SerializableAttribute 
ComVisibleAttribute(true) 
FlagsAttribute 
public enum CompareOptions

 Member nameDescription
Supported by the .NET Compact FrameworkIgnoreCaseIndicates that the string comparison must ignore case. 
Supported by the .NET Compact FrameworkIgnoreKanaTypeIndicates 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. 
Supported by the .NET Compact FrameworkIgnoreNonSpaceIndicates 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. For more information on nonspacing combining characters, see The Unicode Standard at the Unicode home page
Supported by the .NET Compact FrameworkIgnoreSymbolsIndicates 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. 
Supported by the .NET Compact FrameworkIgnoreWidthIndicates 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. 
Supported by the .NET Compact FrameworkNoneIndicates the default option settings for string comparisons. 
Supported by the .NET Compact FrameworkOrdinalIndicates that the string comparison must use the Unicode values of each character, leading to a fast comparison but one that is culture-insensitive. A string starting with "U+xxxx" comes before a string starting with "U+yyyy", if xxxx is less than yyyy. This value cannot be combined with other CompareOptions values and must be used alone. 
Supported by the .NET Compact FrameworkOrdinalIgnoreCaseString 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. 
Supported by the .NET Compact FrameworkStringSortIndicates 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. 

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.

The following code example shows how sorting with StringSort differs from sorting without StringSort.

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

*/

import System.* ;
import System.Collections.* ;
import System.Globalization.* ;

public class SamplesCompareOptions
{
    private class MyStringComparer implements 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;
        } //MyStringComparer

        // 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 =(String) a;
            String sb = (String)b;
            if (sa  != null && sb  != null) {
                return myComp.Compare(sa, sb, myOptions);
            }
            throw new ArgumentException("a and b should be strings.");
        } //Compare 
    } //MyStringComparer
   
    public static void main(String[] args)
    {
        // Creates and initializes an array of strings to sort.
        String myArr[] = new String[]{"cant", "bill's", "coop", "cannot", 
            "billet", "can't", "con", "bills", "co-op"};
        Console.WriteLine("\nInitially,");
            SamplesCompareOptions mySamplesCompareOptions = 
                new SamplesCompareOptions();    
        
        for(int i=0 ; i < myArr.length ;i++) {
            String myStr = myArr[i];
            Console.WriteLine(myStr);
        }
        // Creates and initializes a Comparer to use.
        //CultureInfo myCI = new CultureInfo( "en-US", false );
        MyStringComparer myComp = mySamplesCompareOptions.new MyStringComparer(
            CompareInfo.GetCompareInfo("en-US"), CompareOptions.None);
            
        // Sorts the array without StringSort.
        Array.Sort(myArr, myComp);
        Console.WriteLine("\nAfter sorting without CompareOptions.StringSort:");
        
        for(int i=0; i< myArr.length ;i++) {
            String myStr = myArr[i];
            Console.WriteLine(myStr);
        }
            
        // Sorts the array with StringSort.
        myComp = mySamplesCompareOptions.new MyStringComparer(
            CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort);
        Array.Sort(myArr, myComp);
        Console.WriteLine("\nAfter sorting with CompareOptions.StringSort:");
        
        for(int i=0; i< myArr.length ;i++) {
            String myStr = myArr[i];
            Console.WriteLine(myStr);
        }
    } //main 
} //SamplesCompareOptions

/*
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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft