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 实例是否不相等。

适用于

另请参阅