Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SortKey Class

Represents the result of mapping a string to its sort key.


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

public class SortKey

The SortKey type exposes the following members.

Public propertyKeyDataGets the byte array representing the current SortKey object.
Public propertyOriginalStringGets the original string used to create the current SortKey object.

Public methodStatic memberCompareCompares two sort keys.
Public methodEqualsDetermines whether the specified object is equal to the current SortKey object. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for the current SortKey object that is suitable for hashing algorithms and data structures such as a hash table. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current SortKey object. (Overrides Object.ToString().)

A culture-sensitive comparison of two strings depends on each character in the strings having several categories of sort weights, including script, alphabetic, case, and diacritic weights. A sort key serves as the repository of these weights for a particular string. In particular, the value of a SortKey object is its key data, which is a series of bytes that encode the string, culture-specific sorting rules, and user-specified compare options. A comparison using sort keys consists of a bitwise comparison of the corresponding key data in each sort key.

An instance of the SortKey class is returned by the CompareInfo.GetSortKey method.

Performance Considerations

When performing a string comparison, the SortKey.Compare and CompareInfo.Compare methods yield the same results. However, for an important difference, see the SortKey.Compare and CompareInfo.Compare section.

In effect, the CompareInfo.Compare method generates the sort key for each string, performs the comparison, then discards the sort key and returns the result of the comparison. In fact, the CompareInfo.Compare method does not generate an entire sort key and then perform the comparison. Instead, the method generates the key data for each text element, that is, base character, surrogate pair, or combining character sequence, in each string. The method then compares the key data for the corresponding text elements. The operation terminates as soon as the ultimate result of the comparison is determined. Sort key information is computed, but no SortKey object is created. This strategy is economical in terms of performance if both strings are compared once, but becomes expensive if the same strings are compared many times.

The Compare method requires generation of a SortKey object for each string before performing the comparison. This strategy is expensive in terms of performance for the first comparison because of the time and memory invested to generate the SortKey objects. However, it becomes economical if the same sort keys are compared many times.

For example, suppose you write an application that searches a database table for the row in which the string-based index column matches a specified search string. The table contains thousands of rows, and comparing the search string to the index in each row will take a long time. Therefore, when the application stores a row and its index column, it also generates and stores the sort key for the index in a column dedicated to improving search performance. When the application searches for a target row, it compares the sort key for the search string to the sort key for the index string, instead of comparing the search string to the index string.

SortKey.Compare and CompareInfo.Compare

In general, the SortKey.Compare and CompareInfo.Compare methods return identical results for a particular version of the .NET Framework. However, in the .NET Framework version 4, comparisons that use the CompareOptions.IgnoreWidth option may return inconsistent results. This is particular true for comparisons of the following pairs of characters:



Security Considerations

The CompareInfo.GetSortKey(String, CompareOptions) method returns a SortKey object with the value based on a specified string and CompareOptions value, and the culture associated with the underlying CompareInfo object. If a security decision depends on a string comparison or case change, the application should use the CompareInfo.GetSortKey(String, CompareOptions) method of the invariant culture to ensure that the behavior of the operation is consistent, regardless of the culture settings of the operating system.

The application should use the following steps to obtain the GetSortKey method:

  1. Use the CultureInfo.InvariantCulture property to obtain the invariant culture.

  2. Use the CompareInfo property of the invariant culture to obtain a CompareInfo object.

  3. Use the GetSortKey method of the CompareInfo object.

Working with the value of a SortKey object is equivalent to calling the Windows API LCMapString method with the LCMAP_SORTKEY value specified. However, for the SortKey object, the sort keys for English characters precede the sort keys for Korean characters.

SortKey objects can be serialized, but only so that they can cross AppDomain objects. If an application serializes a SortKey object, the application must regenerate all of the sort keys when there is a new version of the .NET Framework.

For more information about sort keys, see Unicode Technical Standard #10, "Unicode Collation Algorithm" at the Unicode home page.

The following code example compares the string "llama" using the "en-US" and "es-ES" cultures, and the "en-US" and "es-ES" traditional cultures.

using System;
using System.Globalization;

public class SamplesSortKey  {

   public static void Main()  {

      // Creates a SortKey using the en-US culture.
      CompareInfo myComp_enUS = new CultureInfo("en-US",false).CompareInfo;
      SortKey mySK1 = myComp_enUS.GetSortKey( "llama" );

      // Creates a SortKey using the es-ES culture with international sort.
      CompareInfo myComp_esES = new CultureInfo("es-ES",false).CompareInfo;
      SortKey mySK2 = myComp_esES.GetSortKey( "llama" );

      // Creates a SortKey using the es-ES culture with traditional sort.
      CompareInfo myComp_es   = new CultureInfo(0x040A,false).CompareInfo;
      SortKey mySK3 = myComp_es.GetSortKey( "llama" );

      // Compares the en-US SortKey with each of the es-ES SortKey objects.
      Console.WriteLine( "Comparing \"llama\" in en-US and in es-ES with international sort : {0}", SortKey.Compare( mySK1, mySK2 ) );
      Console.WriteLine( "Comparing \"llama\" in en-US and in es-ES with traditional sort   : {0}", SortKey.Compare( mySK1, mySK3 ) );



This code produces the following output.

Comparing "llama" in en-US and in es-ES with international sort : 0
Comparing "llama" in en-US and in es-ES with traditional sort   : -1

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2018 Microsoft