Прочитај на енглеском Уреди

Делите путем


SortVersion Class

Definition

Provides information about the version of Unicode used to compare and order strings.

C#
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
C#
[System.Serializable]
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
Inheritance
SortVersion
Attributes
Implements

Examples

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. 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. If it does not, it creates the data and sets the reindex flag, which indicates that the data must be resorted and saved again. Otherwise, it retrieves the data and compares the saved SortVersion object with the SortVersion object for the current culture on the current system. If they are not equal, or if the reindex flag had been set previously, it resorts to the RegionInfo data.

C#
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);
   }
}

Remarks

For more information about this API, see Supplemental API remarks for SortVersion.

Constructors

SortVersion(Int32, Guid)

Creates a new instance of the SortVersion class.

Properties

FullVersion

Gets the full version number of the SortVersion object.

SortId

Gets a globally unique identifier for this SortVersion object.

Methods

Equals(Object)

Returns a value that indicates whether this SortVersion instance is equal to a specified object.

Equals(SortVersion)

Returns a value that indicates whether this SortVersion instance is equal to a specified SortVersion object.

GetHashCode()

Returns a hash code for this instance.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Operators

Equality(SortVersion, SortVersion)

Indicates whether two SortVersion instances are equal.

Inequality(SortVersion, SortVersion)

Indicates whether two SortVersion instances are not equal.

Applies to

Производ Верзије
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also