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

Comparer.Compare メソッド

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

派生クラスでオーバーライドされると、同じ型の 2 つのオブジェクトに対する比較を実行し、一方のオブジェクトが他方よりも小さいか、等しいか、大きいかを示す値を返します。

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

Public MustOverride Function
 Compare ( _
    x As T, _
    y As T _
) As Integer
Dim instance As Comparer(Of
 T)
Dim x As T
Dim y As T
Dim returnValue As Integer

returnValue = instance.Compare(x, y)

パラメータ

x

比較対象の第 1 オブジェクト。

y

比較対象の第 2 オブジェクト。

戻り値

値

条件

0 より小さい値

x が y より小さい。

0

x と y は等しい。

0 より大きい値

x が y より大きい値です。

例外例外
例外の種類条件

ArgumentException

T 型が、System.IComparable ジェネリック インターフェイスまたは System.IComparable インターフェイスのいずれも実装していません。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Comparer ジェネリック クラス
Comparer メンバ
System.Collections.Generic 名前空間
IComparable ジェネリック インターフェイス
IComparable インターフェイス
StringComparer クラス
Object クラス
System.Globalization.CultureInfo

Comparer.Compare メソッド

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

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

例外例外
例外の種類条件

ArgumentException

a と b が、いずれも IComparable インターフェイスを実装していません。

または

a と b の型が異なっていて、両者を比較できません。

解説解説
使用例使用例

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 名前空間
IComparable インターフェイス
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「Comparer.Compare」の関連用語

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

   

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



Comparer.Compareのページの著作権

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

©2026 GRAS Group, Inc.RSS