Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

SortVersion classe

.NET Framework (current version)
 

Date de publication : novembre 2016

Fournit des informations sur la version Unicode utilisée pour comparer et trier des chaînes.

Espace de noms:   System.Globalization
Assembly:  mscorlib (dans mscorlib.dll)

System.Object
  System.Globalization.SortVersion

[SerializableAttribute]
public sealed class SortVersion : IEquatable<SortVersion>

NomDescription
System_CAPS_pubmethodSortVersion(Int32, Guid)

Crée une instance de la classe SortVersion.

NomDescription
System_CAPS_pubpropertyFullVersion

Obtient le numéro de version complet de le SortVersion objet.

System_CAPS_pubpropertySortId

Obtient un identificateur global unique de cet SortVersion objet.

NomDescription
System_CAPS_pubmethodEquals(Object)

Retourne une valeur qui indique si cette SortVersion instance est égale à un objet spécifié.(Remplace Object.Equals(Object).)

System_CAPS_pubmethodEquals(SortVersion)

Retourne une valeur qui indique si cette SortVersion instance est égale à une certaine SortVersion objet.

System_CAPS_pubmethodGetHashCode()

Retourne un code de hachage pour cette instance.(Remplace Object.GetHashCode().)

System_CAPS_pubmethodGetType()

Obtient le Type de l'instance actuelle.(Hérité de Object.)

System_CAPS_pubmethodToString()

Retourne une chaîne qui représente l'objet actuel.(Hérité de Object.)

NomDescription
System_CAPS_puboperatorSystem_CAPS_staticEquality(SortVersion, SortVersion)

Indique si deux instances de SortVersion sont égales.

System_CAPS_puboperatorSystem_CAPS_staticInequality(SortVersion, SortVersion)

Indique si deux instances de SortVersion ne sont pas égales.

From the net_v20SP1_long through the net_v40_short, 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. In the net_v45, the presence of these tables depends on the operating system:

  • On win7 and previous versions of the Windows operating system, the tables continue to be used for comparing and ordering strings.

  • On win8, the .NET Framework delegates string comparison and ordering operations to the operating system.

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. 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 version

Operating system

Unicode version

.NET Framework 4

All operating systems

Unicode 5.0

.NET Framework 4.5 and later versions of the .NET Framework

Windows 7

Unicode 5.0

.NET Framework 4.5 and later versions of the .NET Framework

Windows 8 and later Windows operating system versions

Unicode 6.0

On win8, 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.

The T:System.Globalization.SortVersion class provides information about the Unicode version used by the .NET Framework for string comparison and ordering. 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.

You can instantiate a T:System.Globalization.SortVersion object in two ways:

  • By calling the M:System.Globalization.SortVersion.#ctor(System.Int32,System.Guid) constructor, which instantiates a new T:System.Globalization.SortVersion object based on a version number and sort ID. This constructor is most useful when recreating a T:System.Globalization.SortVersion object from saved data.

  • By retrieving the value of the P:System.Globalization.CompareInfo.Version property. This property provides information about the Unicode version used by the .NET Framework on which the application is running.

The T:System.Globalization.SortVersion class has two properties, P:System.Globalization.SortVersion.FullVersion and P:System.Globalization.SortVersion.SortId, that indicate the Unicode version and the specific culture used for string comparison. The P:System.Globalization.SortVersion.FullVersion property is an arbitrary numeric value that reflects the Unicode version used for string comparison, and the P:System.Globalization.SortVersion.SortId property is an arbitrary T:System.Guid that reflects the culture whose conventions are used for string comparison. The values of these two properties are important only when you compare two T:System.Globalization.SortVersion objects by using the M:System.Globalization.SortVersion.Equals(System.Globalization.SortVersion) method, the M:System.Globalization.SortVersion.op_Equality(System.Globalization.SortVersion,System.Globalization.SortVersion) operator, or the M:System.Globalization.SortVersion.op_Inequality(System.Globalization.SortVersion,System.Globalization.SortVersion) operator.

You typically use a T:System.Globalization.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. When the ordered string data is saved, the P:System.Globalization.SortVersion.FullVersion and P:System.Globalization.SortVersion.SortId property values are also saved.

  2. When the ordered string data is retrieved, you can recreate the T:System.Globalization.SortVersion object used for ordering the strings by calling the M:System.Globalization.SortVersion.#ctor(System.Int32,System.Guid) constructor.

  3. This newly instantiated T:System.Globalization.SortVersion object is compared with a T:System.Globalization.SortVersion object that reflects the culture whose conventions are used to order the string data.

  4. If the two T:System.Globalization.SortVersion objects are not equal, the string data must be reordered.

The example provides an illustration.

The following example contains a portion of the source code from an application that uses the T:System.Globalization.SortVersion class to ensure that the native names of T:System.Globalization.RegionInfo objects are ordered appropriately for the current system and current culture. It uses the T:System.IO.BinaryReader and T:System.IO.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 T:System.Globalization.SortVersion object with the T:System.Globalization.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 the T:System.Globalization.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.
         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);        
   }
}

.NET Framework
Disponible depuis 4.5

Tous les membres statiques publics ( Shared en Visual Basic) de ce type sont thread-safe. Les membres d’instance n’ont pas la garantie d’être thread-safe.

Retour au début
Afficher: