SortVersion class
This article provides supplementary remarks to the reference documentation for this API.
Sorting and string comparison in .NET Framework
Through .NET Framework 4, each version of .NET Framework included tables that contained sort weights and data on string normalization and that are based on a particular version of Unicode. In .NET Framework 4.5 and later versions, the presence of these tables depends on the operating system:
- On Windows 7 and previous versions, the tables continue to be used for comparing and ordering strings.
- On Windows 8, .NET Framework delegates string comparison and ordering operations to the operating system.
Consequently, the result of a string comparison can depend not only on .NET Framework version, but also on the operating system version, as the following table shows. Note that this list of supported Unicode versions applies to character comparison and sorting only; it does not apply to classification of Unicode characters by category.
.NET Framework version | Operating system | Unicode version |
---|---|---|
4 | All operating systems | Unicode 5.0 |
4.5 and later versions | Windows 7 | Unicode 5.0 |
4.5 and later versions | Windows 8 and later versions | Unicode 6.0 |
On Windows 8, because the version of Unicode used in string comparison and ordering depends on the version of the operating system, the results of string comparison may differ even for applications that run on a specific version of .NET Framework.
Sorting and string comparison in .NET Core
All versions of .NET (Core) rely on the underlying operating system when performing string comparisons. Therefore, the results of a string comparison or the order in which strings are sorted depends on the version of Unicode used by the operating system when performing the comparison. On Linux, macOS, and Windows 10 and later versions, International Components for Unicode libraries provide the implementation for comparison and sorting APIs.
Use the SortVersion class
The SortVersion class provides information about the Unicode version used by .NET for string comparison and ordering. It enables developers to write applications that can detect and successfully handle changes in the version of Unicode that is used to compare and sort an application's strings.
You can instantiate a SortVersion object in two ways:
- By calling the SortVersion constructor, which instantiates a new SortVersion object based on a version number and sort ID. This constructor is most useful when recreating a SortVersion object from saved data.
- By retrieving the value of the CompareInfo.Version property. This property provides information about the Unicode version used by the .NET implementation on which the application is running.
The SortVersion class has two properties, FullVersion and SortId, that indicate the Unicode version and the specific culture used for string comparison. The FullVersion property is an arbitrary numeric value that reflects the Unicode version used for string comparison, and the SortId property is an arbitrary Guid that reflects the culture whose conventions are used for string comparison. The values of these two properties are important only when you compare two SortVersion objects by using the Equals method, the Equality operator, or the Inequality operator.
You typically use a SortVersion object when saving or retrieving some form of culture-sensitive, ordered string data, such as indexes or the literal strings themselves. This requires the following steps:
When the ordered string data is saved, the FullVersion and SortId property values are also saved.
When the ordered string data is retrieved, you can recreate the SortVersion object used for ordering the strings by calling the SortVersion constructor.
This newly instantiated SortVersion object is compared with a SortVersion object that reflects the culture whose conventions are used to order the string data.
If the two SortVersion objects are not equal, the string data must be reordered.