SortVersion Class

Definition

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>
[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)
Inheritance
SortVersion
Attributes
Implements

Remarks

Sorting and string comparison

All versions of .NET (Core) rely on the underlying operating system when performing string comparisons. 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. On Linux, macOS, and Windows 10 and later versions, International Components for Unicode libraries provide the implementation for comparison and sorting APIs.

Use the SortVersion class

The SortVersion class provides information about the Unicode version used by .NET 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 SortVersion object in two ways:

  • By calling the SortVersion constructor, which instantiates a new SortVersion object based on a version number and sort ID. This constructor is most useful when recreating a SortVersion object from saved data.
  • By retrieving the value of the CompareInfo.Version property. This property provides information about the Unicode version used by the .NET implementation on which the application is running.

The SortVersion class has two properties, FullVersion and SortId, that indicate the Unicode version and the specific culture used for string comparison. 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. 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.

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. When the ordered string data is saved, the FullVersion and SortId property values are also saved.

  2. When the ordered string data is retrieved, you can recreate the SortVersion object used for ordering the strings by calling the SortVersion constructor.

  3. 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. If the two SortVersion objects are not equal, the string data must be reordered.

Example

  The following example contains a portion of the source code from an application that uses the <xref:System.Globalization.SortVersion> class to ensure that the native names of <xref:System.Globalization.RegionInfo> objects are ordered appropriately for the current system and current culture. It uses the <xref:System.IO.BinaryReader> and <xref: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 <xref:System.Globalization.SortVersion> object with the <xref:System.Globalization.SortVersion> object for the current culture on the current system. If they aren't equal, or if the `reindex` flag had been set previously, it resorts to the <xref:System.Globalization.RegionInfo> data.

  :::code language="csharp" source="~/snippets/csharp/System.Globalization/SortVersion/Overview/example1.cs" id="Snippet1":::
  :::code language="vb" source="~/snippets/visualbasic/System.Globalization/SortVersion/Overview/example1.vb" id="Snippet1":::

Constructors

Name Description
SortVersion(Int32, Guid)

Creates a new instance of the SortVersion class.

Properties

Name Description
FullVersion

Gets the full version number of the SortVersion object.

SortId

Gets a globally unique identifier for this SortVersion object.

Methods

Name Description
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

Name Description
Equality(SortVersion, SortVersion)

Indicates whether two SortVersion instances are equal.

Inequality(SortVersion, SortVersion)

Indicates whether two SortVersion instances are not equal.

Applies to

See also