Comparer クラス

定義

文字列比較では大文字と小文字が区別される、2 つのオブジェクトを等価性で比較します。

public ref class Comparer sealed : System::Collections::IComparer
public ref class Comparer sealed : System::Collections::IComparer, System::Runtime::Serialization::ISerializable
public sealed class Comparer : System.Collections.IComparer
[System.Serializable]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
type Comparer = class
    interface IComparer
[<System.Serializable>]
type Comparer = class
    interface IComparer
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Comparer = class
    interface IComparer
    interface ISerializable
type Comparer = class
    interface IComparer
    interface ISerializable
Public NotInheritable Class Comparer
Implements IComparer
Public NotInheritable Class Comparer
Implements IComparer, ISerializable
継承
Comparer
属性
実装

次のコード例は、CompareComparerに関連付けられているカルチャに応じて異なる値を返す方法を示しています。

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 インターフェイスの既定の実装です。 CaseInsensitiveComparer クラスは、大文字と小文字を区別しない文字列比較を実行するIComparer インターフェイスの実装です。 System.Collections.Generic.Comparer<T> は、このクラスに相当するジェネリックです。

比較プロシージャでは、特に指定がない限り、現在のスレッドの Thread.CurrentCulture が使用されます。 文字列比較の結果は、カルチャによって異なる場合があります。 カルチャ固有の比較の詳細については、 System.Globalization 名前空間と グローバリゼーションとローカライズに関するページを参照してください。

コンストラクター

名前 説明
Comparer(CultureInfo)

指定したComparerを使用して、CultureInfo クラスの新しいインスタンスを初期化します。

フィールド

名前 説明
Default

現在のスレッドのCurrentCultureに関連付けられているComparerのインスタンスを表します。 このフィールドは読み取り専用です。

DefaultInvariant

InvariantCultureに関連付けられているComparerのインスタンスを表します。 このフィールドは読み取り専用です。

メソッド

名前 説明
Compare(Object, Object)

同じ型の 2 つのオブジェクトの大文字と小文字を区別して比較を実行し、1 つが他方より小さいか、等しいか、または大きいかを示す値を返します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetObjectData(SerializationInfo, StreamingContext)

シリアル化に必要なデータを SerializationInfo オブジェクトに設定します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください