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.

Performance Considerations

In effect, the System.Globalization.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 System.Globalization.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, and 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 generating 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, but 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 whose string-based index column matches a specified search string. The table contains hundreds of thousands of rows, and comparing the search string to the index in each row would take a long time. Instead, when you store a row and its index column, you also generate and store the sort key for the index in a column dedicated to improving search performance. Whenever you search for a target row, your application 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.

Security Considerations

The CompareInfo.GetSortKey(String,CompareOptions) method returns a SortKey object whose value is 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 operation, 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 system.

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

Use the GetSortKey method of the CompareInfo object.

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

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