SortVersion SortVersion SortVersion 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>
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、 每個版本的.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. 這兩個屬性的值很重要,只有在您比較兩個時,才SortVersion使用的物件Equals方法,Equality運算子或Inequality運算子。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. 儲存的已排序的字串資料時,FullVersionSortId也會儲存屬性值。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(Int32, Guid) SortVersion(Int32, Guid) SortVersion(Int32, Guid)

建立 SortVersion 類別的新執行個體。Creates a new instance of the SortVersion class.


FullVersion FullVersion FullVersion FullVersion

取得 SortVersion 物件的完整版本號碼。Gets the full version number of the SortVersion object.

SortId SortId SortId SortId

取得這個 SortVersion 物件的全域唯一識別項。Gets a globally unique identifier for this SortVersion object.


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,指出這個 SortVersion 執行個體是否等於指定的物件。Returns a value that indicates whether this SortVersion instance is equal to a specified object.

Equals(SortVersion) Equals(SortVersion) Equals(SortVersion) Equals(SortVersion)

傳回值,指出這個 SortVersion 執行個體是否等於指定的 SortVersion 物件。Returns a value that indicates whether this SortVersion instance is equal to a specified SortVersion object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns a hash code for this instance.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)


Equality(SortVersion, SortVersion) Equality(SortVersion, SortVersion) Equality(SortVersion, SortVersion) Equality(SortVersion, SortVersion)

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

Inequality(SortVersion, SortVersion) Inequality(SortVersion, SortVersion) Inequality(SortVersion, SortVersion) Inequality(SortVersion, SortVersion)

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