Comparer Comparer Comparer Comparer Class

定义

比较两个对象是否相等,其中字符串比较是区分大小写的。Compares two objects for equivalence, where string comparisons are case-sensitive.

public ref class Comparer sealed : System::Collections::IComparer, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
type Comparer = class
    interface IComparer
    interface ISerializable
Public NotInheritable Class Comparer
Implements IComparer, ISerializable
继承
ComparerComparerComparerComparer
属性
实现

示例

下面的代码示例演示如何Compare返回不同的值, 具体取决于Comparer与关联的区域性。The following code example shows how Compare returns different values depending on the culture associated with the Comparer.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
   
   // Creates the strings to compare.
   String^ str1 = "llegar";
   String^ str2 = "lugar";
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );
   
   // Uses the DefaultInvariant Comparer.
   Console::WriteLine( "   Invariant Comparer: {0}", Comparer::DefaultInvariant->Compare( str1, str2 ) );
   
   // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
   Comparer^ myCompIntl = gcnew Comparer( gcnew CultureInfo(  "es-ES",false ) );
   Console::WriteLine( "   International Sort: {0}", myCompIntl->Compare( str1, str2 ) );
   
   // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
   Comparer^ myCompTrad = gcnew Comparer( gcnew CultureInfo( 0x040A,false ) );
   Console::WriteLine( "   Traditional Sort  : {0}", myCompTrad->Compare( str1, str2 ) );
}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
using System;
using System.Collections;
using System.Globalization;


public class SamplesComparer  {

   public static void Main()  {

      // Creates the strings to compare.
      String str1 = "llegar";
      String str2 = "lugar";
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );

      // Uses the DefaultInvariant Comparer.
      Console.WriteLine( "   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Comparer myCompIntl = new Comparer( new CultureInfo( "es-ES", false ) );
      Console.WriteLine( "   International Sort: {0}", myCompIntl.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Comparer myCompTrad = new Comparer( new CultureInfo( 0x040A, false ) );
      Console.WriteLine( "   Traditional Sort  : {0}", myCompTrad.Compare( str1, str2 ) );

   }

}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
Imports System.Collections
Imports System.Globalization

Public Class SamplesComparer

   Public Shared Sub Main()

      ' Creates the strings to compare.
      Dim str1 As [String] = "llegar"
      Dim str2 As [String] = "lugar"
      Console.WriteLine("Comparing ""{0}"" and ""{1}"" ...", str1, str2)

      ' Uses the DefaultInvariant Comparer.
      Console.WriteLine("   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare(str1, str2))

      ' Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Dim myCompIntl As New Comparer(New CultureInfo("es-ES", False))
      Console.WriteLine("   International Sort: {0}", myCompIntl.Compare(str1, str2))

      ' Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Dim myCompTrad As New Comparer(New CultureInfo(&H40A, False))
      Console.WriteLine("   Traditional Sort  : {0}", myCompTrad.Compare(str1, str2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
'   Invariant Comparer: -1
'   International Sort: -1
'   Traditional Sort  : 1

注解

此类是IComparer接口的默认实现。This class is the default implementation of the IComparer interface. 类是IComparer接口的实现, 该接口执行不区分大小写的字符串比较。 CaseInsensitiveComparerThe CaseInsensitiveComparer class is the implementation of the IComparer interface that performs case-insensitive string comparisons. System.Collections.Generic.Comparer<T>是此类的泛型等效项。System.Collections.Generic.Comparer<T> is the generic equivalent of this class.

除非另行指定, Thread.CurrentCulture否则比较过程使用当前线程的。Comparison procedures use the Thread.CurrentCulture of the current thread unless otherwise specified. 根据区域性, 字符串比较可能会产生不同的结果。String comparisons might have different results depending on the culture. 有关区域性特定比较的详细信息, 请参阅System.Globalization命名空间和全球化和本地化For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

构造函数

Comparer(CultureInfo) Comparer(CultureInfo) Comparer(CultureInfo) Comparer(CultureInfo)

使用指定的 Comparer 初始化 CultureInfo 类的新实例。Initializes a new instance of the Comparer class using the specified CultureInfo.

字段

Default Default Default Default

表示 Comparer 的实例,它与当前线程的 CurrentCulture 关联。Represents an instance of Comparer that is associated with the CurrentCulture of the current thread. 此字段为只读。This field is read-only.

DefaultInvariant DefaultInvariant DefaultInvariant DefaultInvariant

表示 Comparer 的实例,它与 InvariantCulture 关联。Represents an instance of Comparer that is associated with InvariantCulture. 此字段为只读。This field is read-only.

方法

Compare(Object, Object) Compare(Object, Object) Compare(Object, Object) Compare(Object, Object)

对同一类型的两个对象执行区分大小写的比较,并返回一个值,指示其中一个对象小于、等于还是大于另一个对象。Performs a case-sensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

用序列化所需的数据填充 SerializationInfo 对象。Populates a SerializationInfo object with the data required for serialization.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅