Comparerとは? わかりやすく解説

Comparer クラス

2 つオブジェクト等しかどうか比較します。文字列大文字と小文字区別されます。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 Comparer
    Implements IComparer, ISerializable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class Comparer : IComparer, ISerializable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Comparer sealed : IComparer,
 ISerializable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class Comparer implements IComparer,
 ISerializable
SerializableAttribute 
ComVisibleAttribute(true) 
public final class Comparer implements IComparer,
 ISerializable
解説解説
使用例使用例

Comparer関連付けられているカルチャに応じてCompare返す値が変わる方法については、次のコードの例参照してください

Imports System
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

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

*/
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

*/
import System.* ;
import System.Collections.*;
import System.Globalization.*;

public class SamplesComparer
{
    public static void main(String[]
 args)
    {
        // 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}", 
            (Int32)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}", 
            (Int32)myCompIntl.Compare(str1, str2));

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

/*
This code produces the following output.

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

*/
継承階層継承階層
System.Object
  System.Collections.Comparer
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Comparer メンバ
System.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo

Comparer コンストラクタ


Comparer コンストラクタ

System.Globalization.CultureInfo指定してComparer クラス新しインスタンス初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    culture As CultureInfo _
)
Dim culture As CultureInfo

Dim instance As New Comparer(culture)
public Comparer (
    CultureInfo culture
)
public:
Comparer (
    CultureInfo^ culture
)
public Comparer (
    CultureInfo culture
)
public function Comparer (
    culture : CultureInfo
)

パラメータ

culture

新しい Comparer で使用する System.Globalization.CultureInfo。

例外例外
例外種類条件

ArgumentNullException

culturenull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

Comparer関連付けられているカルチャに応じてCompare返す値が変わる方法については、次のコードの例参照してください

Imports System
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

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

*/
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

*/
import System.* ;
import System.Collections.*;
import System.Globalization.*;

public class SamplesComparer
{
    public static void main(String[]
 args)
    {
        // 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}", 
            (Int32)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}", 
            (Int32)myCompIntl.Compare(str1, str2));

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

/*
This code produces the following output.

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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Comparer クラス
Comparer メンバ
System.Collections 名前空間
System.Globalization.CultureInfo
System.Globalization.CompareInfo

Comparer ジェネリック クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

IComparer ジェネリック インターフェイス実装のための基本クラス提供します

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
Public MustInherit Class
 Comparer(Of T)
    Implements IComparer, IComparer(Of T)
Dim instance As Comparer(Of
 T)
[SerializableAttribute] 
public abstract class Comparer<T> : IComparer,
 IComparer<T>
[SerializableAttribute] 
generic<typename T>
public ref class Comparer abstract : IComparer,
 IComparer<T>
J# では、ジェネリックな型およびメソッド使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッド使用できません。

型パラメータ

T

比較するオブジェクトの型。

解説解説

このクラスから派生して、SortedList ジェネリック クラスや SortedDictionary ジェネリック クラスなどのコレクション クラス使用される IComparer インターフェイスカスタム実装提供します

Default プロパティによって返されるオブジェクトでは、System.IComparable ジェネリック インターフェイス (C# では IComparable<T>Visual Basic では IComparable(Of T)) を使用して 2 つオブジェクト比較されます。T 型が System.IComparable ジェネリック インターフェイス実装ていない場合Default プロパティは、System.IComparable インターフェイス使用する Comparer返します

実装時の注意 Compare と EqualityComparer.Equals は、カルチャの依存性、および大文字小文字の区別の点で動作異な場合あります文字列比較場合は、Comparer<String> に StringComparer クラス使用することをお勧めます。StringComparer クラスプロパティは、カルチャの依存性大文字と小文字区別をさまざまに組み合わせることによって文字列比較実行する、定義済みインスタンス返します大文字小文字の区別およびカルチャの依存性は、StringComparer の同じインスタンスすべてのメンバ共有されます。 カルチャ固有の比較詳細については、System.Globalization 名前空間トピックおよび「エンコーディングローカリゼーション」を参照してください

継承階層継承階層
System.Object
  System.Collections.Generic.Comparer
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Comparer メンバ
System.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo

Comparer フィールド


パブリック フィールドパブリック フィールド

  名前 説明
パブリック フィールド DefaultInvariant CultureInfo.InvariantCulture に関連付けられた Comparerインスタンス表します。このフィールド読み取り専用です。
参照参照

関連項目

Comparer クラス
System.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo

Comparer プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

Comparer ジェネリック クラス
System.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo

Comparer メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IComparer.Compare 2 つオブジェクト比較し一方他方より小さいか、等しいか、大きいかを示す値を返します
参照参照

関連項目

Comparer ジェネリック クラス
System.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo

Comparer メソッド


パブリック メソッドパブリック メソッド

参照参照

関連項目

Comparer クラス
System.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo

Comparer メンバ

IComparer ジェネリック インターフェイス実装のための基本クラス提供します

Comparer ジェネリック型公開されるメンバを以下の表に示します


プロテクト コンストラクタプロテクト コンストラクタ
  名前 説明
プロテクト メソッド Comparer Comparer クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IComparer.Compare 2 つオブジェクト比較し一方他方より小さいか、等しいか、大きいかを示す値を返します
参照参照

関連項目

Comparer ジェネリック クラス
System.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo

Comparer メンバ

2 つオブジェクト等しかどうか比較します。文字列大文字と小文字区別されます。

Comparer データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド Comparer System.Globalization.CultureInfo を指定して、Comparer クラス新しインスタンス初期化します。
パブリック フィールドパブリック フィールド
  名前 説明
パブリック フィールド DefaultInvariant CultureInfo.InvariantCulture に関連付けられた Comparerインスタンス表します。このフィールド読み取り専用です。
パブリック メソッドパブリック メソッド
参照参照

関連項目

Comparer クラス
System.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Comparer」の関連用語

Comparerのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Comparerのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS