共用方式為


SortVersion 類別

定義

提供用於比較和排序字串之 Unicode 版本的相關資訊。

public ref class SortVersion sealed : IEquatable<System::Globalization::SortVersion ^>
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
[System.Serializable]
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
type SortVersion = class
    interface IEquatable<SortVersion>
[<System.Serializable>]
type SortVersion = class
    interface IEquatable<SortVersion>
Public NotInheritable Class SortVersion
Implements IEquatable(Of SortVersion)
繼承
SortVersion
屬性
實作

範例

下列範例包含來自使用 類別的應用程式 SortVersion 原始程式碼的一部分,以確保物件的原生名稱 RegionInfo 會適當地針對目前的系統和目前的文化特性進行排序。 它會使用 BinaryReaderBinaryWriter 對象來儲存和擷取名為 Regions.dat 的數據檔中已排序的數據,而不是每次執行應用程式時擷取和排序數據。 此範例會先檢查,以判斷數據檔是否存在。 如果沒有,它會建立數據並設定 reindex 旗標,這表示必須重新取用並儲存數據。 否則,它會擷取數據,並將儲存 SortVersion 的物件與 SortVersion 目前系統上目前文化特性的對象進行比較。 如果它們不相等,或 reindex 先前已設定旗標,則會依賴 RegionInfo 數據。

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.
         SaveData(regions);
      }

      // 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))
            continue;

         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));
      wrtr.Write(ver.FullVersion);
      wrtr.Write(ver.SortId.ToString());

      foreach (var region in regions) {
         wrtr.Write(region.Id);
         wrtr.Write(region.NativeName);
      }
      wrtr.Close();
   }

   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,
                              StringComparison.CurrentCulture);
   }
}
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.
      Else
         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.
         SaveData(regions)
      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))
      Next
      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))      
      Loop
      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))
      wrtr.Write(ver.FullVersion) 
      wrtr.Write(ver.SortId.ToString()) 
      
      For Each region In regions
         wrtr.Write(region.Id)
         wrtr.Write(region.NativeName)
      Next
      wrtr.Close()
   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, 
                              StringComparison.CurrentCulture)         
   End Function
End Class

備註

如需此 API 的詳細資訊,請參閱 SortVersion 的補充 API 備註

建構函式

SortVersion(Int32, Guid)

建立 SortVersion 類別的新執行個體。

屬性

FullVersion

取得 SortVersion 物件的完整版本號碼。

SortId

取得這個 SortVersion 物件的全域唯一識別項。

方法

Equals(Object)

傳回值,指出這個 SortVersion 執行個體是否等於指定的物件。

Equals(SortVersion)

傳回值,指出這個 SortVersion 執行個體是否等於指定的 SortVersion 物件。

GetHashCode()

傳回這個執行個體的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

運算子

Equality(SortVersion, SortVersion)

表示兩個 SortVersion 執行個體是否相等。

Inequality(SortVersion, SortVersion)

表示兩個 SortVersion 執行個體是否不相等。

適用於

另請參閱