SortVersion 类


提供用于比较和排序字符串的 Unicode 版本的相关信息。Provides information about the version of Unicode used to compare and order strings.

public ref class SortVersion sealed : IEquatable<System::Globalization::SortVersion ^>
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
type SortVersion = class
    interface IEquatable<SortVersion>
type SortVersion = class
    interface IEquatable<SortVersion>
Public NotInheritable Class SortVersion
Implements IEquatable(Of SortVersion)


下面的示例包含来自使用类的应用程序的部分源代码, SortVersion 以确保对象的本机名称 RegionInfo 按正确的顺序针对当前系统和当前区域性进行排序。The following example contains a portion of the source code from an application that uses the SortVersion class to ensure that the native names of RegionInfo objects are ordered appropriately for the current system and current culture. 它使用 BinaryReaderBinaryWriter 对象来存储和检索名为的数据文件中的有序数据, Regions.dat 而不是每次运行应用程序时检索和排序数据。It uses the BinaryReader and BinaryWriter objects to store and retrieve ordered data from a data file named Regions.dat rather than retrieving and ordering data each time the application is run. 该示例首先检查以确定数据文件是否存在。The example first checks to determine whether the data file exists. 如果不是,它将创建数据并设置 reindex 标志,这表示必须重新对数据进行排序和保存。If it does not, it creates the data and sets the reindex flag, which indicates that the data must be resorted and saved again. 否则,它将检索数据并将保存的 SortVersion 对象与 SortVersion 当前系统上的当前区域性的对象进行比较。Otherwise, it retrieves the data and compares the saved SortVersion object with the SortVersion object for the current culture on the current system. 如果它们不相等,或 reindex 以前设置了标志,则会对数据进行排序 RegionInfoIf they are not equal, or if the reindex flag had been set previously, it resorts the RegionInfo data.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

public class Example : IComparer
   private const string FILENAME = @".\Regions.dat";

   private struct Region
      internal Region(string id, string name)
         this.Id = id;
         this.NativeName = name;

      public string Id;
      public string NativeName;

      public override string ToString()
         return this.NativeName;

   public static void Main()
      bool reindex = false;

      Region[] regions;
      SortVersion ver = null;

      // If the data has not been saved, create it.
      if (! File.Exists(FILENAME)) {
         regions = GenerateData();
         ver = CultureInfo.CurrentCulture.CompareInfo.Version;
         reindex = true;
      // Retrieve the existing data.
      else {
         regions = RestoreData(out ver);

      // Determine whether the current ordering is valid; if not, reorder.
      if (reindex || ver != CultureInfo.CurrentCulture.CompareInfo.Version) {
         Array.Sort(regions, new Example());
         // Save newly reordered data.

      // Continue with application...

   private static Region[] GenerateData()
      List<Region> regions = new List<Region>();

      foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
         if (culture.IsNeutralCulture | culture.Equals(CultureInfo.InvariantCulture))

         RegionInfo region = new RegionInfo(culture.Name);
         regions.Add(new Region(region.Name, region.NativeName));
      return regions.ToArray();

   private static Region[] RestoreData(out SortVersion ver)
      List<Region> regions = new List<Region>();

      BinaryReader rdr = new BinaryReader(File.Open(FILENAME, FileMode.Open));

      int sortVer = rdr.ReadInt32();
      Guid sortId = Guid.Parse(rdr.ReadString());
      ver = new SortVersion(sortVer, sortId);

      string id, name;
      while (rdr.PeekChar() != -1) {
         id = rdr.ReadString();
         name = rdr.ReadString();
         regions.Add(new Region(id, name));
      return regions.ToArray();

   private static void SaveData(Region[] regions)
      SortVersion ver = CultureInfo.CurrentCulture.CompareInfo.Version;

      BinaryWriter wrtr = new BinaryWriter(File.Open(FILENAME, FileMode.Create));

      foreach (var region in regions) {

   public int Compare(object o1, object o2)
        // Assume that all casts succeed.
        Region r1 = (Region) o1;
        Region r2 = (Region) o2;

        return String.Compare(r1.NativeName, r2.NativeName,
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports System.Text

Public Class Example : Implements IComparer
   Private Const FILENAME As String = ".\Regions.dat"
   Private Structure Region
      Friend Sub New(id As String, name As String)
         Me.Id = id
         Me.NativeName = name
      End Sub
      Dim Id As String
      Dim NativeName As String
      Public Overrides Function ToString() As String
         Return Me.NativeName
      End Function
   End Structure
   Public Shared Sub Main()
      Dim reindex As Boolean = False
      Dim regions() As Region
      Dim ver As SortVersion = Nothing

      ' If the data has not been saved, create it.
      If Not File.Exists(FILENAME) Then 
         regions = GenerateData()
         ver = CultureInfo.CurrentCulture.CompareInfo.Version  
         reindex = True
      ' Retrieve the existing data.
         regions = RestoreData(ver)
      End If

      ' Determine whether the current ordering is valid; if not, reorder.
      If reindex OrElse ver <> CultureInfo.CurrentCulture.CompareInfo.Version Then 
         Array.Sort(regions, New Example())      
         ' Save newly reordered data.
      End If
      ' Continue with application...
   End Sub

   Private Shared Function GenerateData() As Region()
      Dim regions As New List(Of Region)()

      For Each culture In CultureInfo.GetCultures(CultureTypes.AllCultures)
         If culture.IsNeutralCulture Or culture.Equals(CultureInfo.InvariantCulture) Then Continue For
         Dim region As New RegionInfo(culture.Name)
         regions.Add(New Region(region.Name, region.NativeName))
      Return regions.ToArray()
   End Function
   Private Shared Function RestoreData(ByRef ver As SortVersion) As Region()
      Dim regions As New List(Of Region)
      Dim rdr As New BinaryReader(File.Open(FILENAME, FileMode.Open))
      Dim sortVer As Integer = rdr.ReadInt32
      Dim sortId As Guid = Guid.Parse(rdr.ReadString())
      ver = New SortVersion(sortVer, sortId)
      Dim id As String, name As String
      Do While rdr.PeekChar <> -1
         id = rdr.ReadString()
         name = rdr.ReadString()
         regions.Add(New Region(id, name))      
      Return regions.ToArray()
   End Function
   Private Shared Sub SaveData(regions As Region())
      Dim ver As SortVersion = CultureInfo.CurrentCulture.CompareInfo.Version

      Dim wrtr As New BinaryWriter(File.Open(FILENAME, FileMode.Create))
      For Each region In regions
   End Sub

   Private Function SortByNativeName(o1 As Object, o2 As Object) As Integer _
           Implements IComparer.Compare
        ' Assume that all conversions succeed.
        Dim r1 As Region = CType(o1, Region)
        Dim r2 As Region = CType(o2, Region)
        Return String.Compare(r1.NativeName, r2.NativeName, 
   End Function
End Class


.NET Framework 中的排序和字符串比较Sorting and string comparison in the .NET Framework

从 .NET Framework 2.0 Service Pack 1 到 .NET Framework 4,the.NET Framework 的每个版本都包含一些表,这些表包含针对字符串规范化的排序权重和数据,并基于特定版本的 Unicode。From the .NET Framework 2.0 Service Pack 1 through the .NET Framework 4, each version of the.NET Framework has included tables that contain sort weights and data on string normalization and that are based on a particular version of Unicode. 在 .NET Framework 4.5 中,这些表的存在取决于操作系统:In the .NET Framework 4.5, the presence of these tables depends on the operating system:

  • 在 windows 7 和以前版本的 Windows 操作系统上,表继续用于比较和排序字符串。On Windows 7 and previous versions of the Windows operating system, the tables continue to be used for comparing and ordering strings.

  • 在 Windows 8 中,.NET Framework 将字符串比较和排序操作委托给操作系统。On Windows 8, the .NET Framework delegates string comparison and ordering operations to the operating system.

因此,字符串比较的结果不仅可依赖于 .NET Framework 版本,还取决于操作系统版本,如下表所示。Consequently, the result of a string comparison can depend not only on the .NET Framework version, but also on the operating system version, as the following table shows. 请注意,受支持的 Unicode 版本的列表仅适用于字符比较和排序;不适用于 Unicode 字符串按类别分类。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 版本.NET Framework version 操作系统Operating system Unicode 版本Unicode version
.NET Framework 4.NET Framework 4 所有操作系统All operating systems Unicode 5.0Unicode 5.0
.NET Framework 4.5 及更高版本的 .NET Framework.NET Framework 4.5 and later versions of the .NET Framework Windows 7Windows 7 Unicode 5.0Unicode 5.0
.NET Framework 4.5 及更高版本的 .NET Framework.NET Framework 4.5 and later versions of the .NET Framework Windows 8 及更高版本的 Windows 操作系统版本Windows 8 and later Windows operating system versions Unicode 6.0Unicode 6.0

在 Windows 8 上,因为字符串比较和排序中使用的 Unicode 版本依赖于操作系统的版本,所以,即使对于在特定版本的 .NET Framework 上运行的应用程序,字符串比较的结果也可能不同。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 the .NET Framework.

.NET Core 中的排序和字符串比较Sorting and string comparison in .NET Core

在执行字符串比较时,所有版本的 .NET Core 依赖于基础操作系统。All versions of .NET Core rely on the underlying operating system when performing string comparisons. 因此,字符串比较的结果或字符串的排序顺序取决于执行比较时操作系统使用的 Unicode 版本。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. 在 Linux 和 macOS 上,Unicode 库的 国际组件 提供用于比较和排序 api 的实现。On Linux and macOS, International Components for Unicode libraries provide the implementation for comparison and sorting APIs.

使用 SortVersion 类Using the SortVersion class

SortVersion类提供有关 .net 用于字符串比较和排序的 Unicode 版本的信息。The SortVersion class provides information about the Unicode version used by .NET for string comparison and ordering. 它使开发人员能够编写应用程序,这些应用程序可以检测和成功处理用于对应用程序字符串进行比较和排序的 Unicode 版本的更改。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.

可以 SortVersion 通过两种方式实例化对象:You can instantiate a SortVersion object in two ways:

  • 通过调用 SortVersion 构造函数,该构造函数 SortVersion 基于版本号和排序 ID 实例化新的对象。By calling the SortVersion constructor, which instantiates a new SortVersion object based on a version number and sort ID. 在从保存的数据重新创建对象时,此构造函数最有用 SortVersionThis constructor is most useful when recreating a SortVersion object from saved data.

  • 通过检索属性的值 CompareInfo.VersionBy retrieving the value of the CompareInfo.Version property. 此属性提供有关在其上运行应用程序的 .NET 实现所使用的 Unicode 版本的信息。This property provides information about the Unicode version used by the .NET implementation on which the application is running.

SortVersion类具有两个属性: FullVersionSortId ,它们指示 Unicode 版本和用于字符串比较的特定区域性。The SortVersion class has two properties, FullVersion and SortId, that indicate the Unicode version and the specific culture used for string comparison. FullVersion属性是一个任意数值,该数值反映用于字符串比较的 Unicode 版本, SortId 属性是一个任意 Guid 的,它反映其约定用于字符串比较的区域性。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. 仅当 SortVersion 使用 Equals 方法、 Equality 运算符或运算符比较两个对象时,这两个属性的值才是重要的 InequalityThe 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.

通常在 SortVersion 保存或检索某种形式的区分区域性的、有序的字符串数据(如索引或文字字符串本身)时使用对象。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:

  1. 保存有序字符串数据时, FullVersion SortId 也会保存和属性值。When the ordered string data is saved, the FullVersion and SortId property values are also saved.

  2. 检索有序字符串数据时,可以 SortVersion 通过调用构造函数来重新创建用于对字符串进行排序的对象 SortVersionWhen the ordered string data is retrieved, you can recreate the SortVersion object used for ordering the strings by calling the SortVersion constructor.

  3. 此新实例化的 SortVersion 对象与一个 SortVersion 对象进行比较,该对象反映其约定用于对字符串数据进行排序的区域性。This newly instantiated SortVersion object is compared with a SortVersion object that reflects the culture whose conventions are used to order the string data.

  4. 如果两个 SortVersion 对象不相等,则必须对字符串数据进行重新排序。If the two SortVersion objects are not equal, the string data must be reordered.

说明如示例所示。The example provides an illustration.


SortVersion(Int32, Guid)

创建 SortVersion 类的新实例。Creates a new instance of the SortVersion class.



获取 SortVersion 对象的全版本号。Gets the full version number of the SortVersion object.


获取此 SortVersion 对象的全局唯一标识符。Gets a globally unique identifier for this SortVersion object.



返回一个值,该值指示此 SortVersion 实例是否与指定的对象相等。Returns a value that indicates whether this SortVersion instance is equal to a specified object.


返回一个值,该值指示此 SortVersion 实例是否与指定的 SortVersion 对象相等。Returns a value that indicates whether this SortVersion instance is equal to a specified SortVersion object.


返回此实例的哈希代码。Returns a hash code for this instance.


获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)


Equality(SortVersion, SortVersion)

指示两个 SortVersion 实例是否相等。Indicates whether two SortVersion instances are equal.

Inequality(SortVersion, SortVersion)

指示两个 SortVersion 实例是否不相等。Indicates whether two SortVersion instances are not equal.