Comparer Comparer Comparer Comparer Class

定義

2 つのオブジェクトが等しいかどうかを比較します。文字列の大文字と小文字は区別されます。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 'Main 

End Class 'SamplesComparer


'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)

指定した CultureInfo を使用して Comparer クラスの新しいインスタンスを初期化します。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)

同じ型の 2 つのオブジェクトに対して大文字と小文字を区別する比較を実行し、一方が他方よりも小さいか、等しいか、大きいかを示す値を返します。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()

現在のインスタンスの Type を取得します。Gets 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)

適用対象

こちらもご覧ください