SortVersion Class


提供用於比較和排序字串之 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>
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 旗標,則會將 RegionInfo 資料。If 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 2.0 Service Pack 1.NET Framework 4.NET Framework 4,the.NET Framework 的每個版本都包含包含排序權數的資料表,以及字串正規化和以特定 Unicode 版本為基礎的資料。From the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1 through the .NET Framework 4.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.NET Framework 4.5中,這些資料表的存在取決於作業系統:In the .NET Framework 4.5.NET Framework 4.5, the presence of these tables depends on the operating system:

  • Windows 7Windows 7 和舊版的 Windows 作業系統上,資料表會繼續用於比較和排序字串。On Windows 7Windows 7 and previous versions of the Windows operating system, the tables continue to be used for comparing and ordering strings.

  • Windows 8Windows 8上,.NET Framework 會將字串比較和排序作業委派給作業系統。On Windows 8Windows 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 4.5 和更新版本的 .NET Frameworkand later versions of the .NET Framework Windows 7Windows 7 Unicode 5.0Unicode 5.0
.NET Framework 4.5.NET Framework 4.5 和更新版本的 .NET Frameworkand later versions of the .NET Framework Windows 8Windows 8 和更新版本的 Windows 作業系統版本and later Windows operating system versions Unicode 6.0Unicode 6.0

Windows 8Windows 8上,由於字串比較和順序中使用的 Unicode 版本取決於作業系統的版本,因此字串比較的結果可能會不同,即使是在特定版本的 .NET Framework 上執行的應用程式也一樣。On Windows 8Windows 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 物件。By calling the SortVersion constructor, which instantiates a new SortVersion object based on a version number and sort ID. 從儲存的資料重新建立 SortVersion 物件時,此函式最有用。This constructor is most useful when recreating a SortVersion object from saved data.

  • 藉由抓取 CompareInfo.Version 屬性的值。By 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. 只有當您使用 Equals 方法、Equality 運算子或 Inequality 運算子比較兩個 SortVersion 物件時,這兩個屬性的值才很重要。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.

當您儲存或抓取某種形式的區分文化特性、已排序的字串資料(例如索引或常值字串本身)時,通常會使用 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 的「函式」,重新建立用來排序字串的 SortVersion 物件。When 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.

(Inherited from Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)


Equality(SortVersion, SortVersion)

表示兩個 SortVersion 執行個體是否相等。Indicates whether two SortVersion instances are equal.

Inequality(SortVersion, SortVersion)

表示兩個 SortVersion 執行個體是否不相等。Indicates whether two SortVersion instances are not equal.

Applies to

See also