Export (0) Print
Expand All

CompareOptions Enumeration

Defines the options to use with a number of string comparison methods.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration CompareOptions

Member nameDescription
Supported by Silverlight for Windows PhoneSupported by Xbox 360NoneIndicates the default option settings for string comparisons.
Supported by Silverlight for Windows PhoneSupported by Xbox 360IgnoreCaseIndicates that the string comparison must ignore case.
Supported by Silverlight for Windows PhoneSupported by Xbox 360IgnoreNonSpaceIndicates 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 Silverlight for Windows PhoneSupported by Xbox 360IgnoreSymbolsIndicates 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 Silverlight for Windows PhoneSupported by Xbox 360IgnoreKanaTypeIndicates 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 Silverlight for Windows PhoneSupported by Xbox 360IgnoreWidthIndicates 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 Silverlight for Windows PhoneSupported by Xbox 360OrdinalIgnoreCaseString 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 Silverlight for Windows PhoneSupported by Xbox 360StringSortIndicates 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.
Supported by Silverlight for Windows PhoneSupported by Xbox 360OrdinalIndicates 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.

The members of the CompareOptions enumeration are used with a number of CompareInfo methods, as well as with several overloads of the Compare method. They denote case sensitivity or the 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 CompareOptions.StringSort value can be used only with CompareInfo.Compare and the appropriate overloads of Compare. An 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.


Imports System.Collections
Imports System.Globalization

Public Class Example

   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(ByVal cmpi As CompareInfo, ByVal options As CompareOptions)
         myComp = cmpi
         Me.myOptions = options
      End Sub 'New

      ' Compares strings with the CompareOptions specified in the constructor.
      Public Function Compare(ByVal a As [Object], ByVal 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 Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' 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"}
      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("Initially,") & vbCrLf
      Dim myStr As [String]
      For Each myStr In myArr
         outputBlock.Text &= myStr & vbCrLf
      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)
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= "After sorting without CompareOptions.StringSort:" & vbCrLf
      For Each myStr In myArr
         outputBlock.Text &= myStr & vbCrLf
      Next myStr

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

   End Sub 'Main

End Class '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



Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft