次の方法で共有


Comparer.Compare メソッド

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

Public Overridable Function Compare( _
   ByVal a As Object, _   ByVal b As Object _) As Integer Implements IComparer.Compare
[C#]
public virtual int Compare(objecta,objectb);
[C++]
public: virtual int Compare(Object* a,Object* b);
[JScript]
public function Compare(
   a : Object,b : Object) : int;

パラメータ

  • a
    比較対象の第 1 オブジェクト。
  • b
    比較対象の第 2 オブジェクト。

戻り値

説明
0 より小さい値 ab より小さい。
0 ab は等しい。
0 より大きい値 ab より大きい。

実装

IComparer.Compare

例外

例外の種類 条件
ArgumentException ab が、いずれも IComparable インターフェイスを実装していません。

または

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

解説

aIComparable を実装する場合は、 a. CompareTo (b) が返されます。逆に、 bIComparable を実装する場合は、符号反転した b. CompareTo (a) が返されます。

null 参照 (Visual Basic では Nothing) を任意の型と比較できます。このような比較を行っても、 IComparable を使用した場合に例外が生成されることはありません。並べ替え処理では、 null 参照 (Nothing) は、他のすべてのオブジェクトより小さいと見なされます。

文字列比較の結果は、カルチャに応じて異なる場合があります。カルチャ固有の比較の詳細については、 System.Globalization 名前空間のトピックおよび「 国際対応アプリケーションの開発 」を参照してください。

使用例

[Visual Basic, C#, C++] 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


[C#] 
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

*/

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

int main()  {

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

    // Uses the DefaultInvariant Comparer.
    Console::WriteLine(S"   Invariant Comparer: {0}", __box(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(S"   International Sort: {0}", __box(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(S"   Traditional Sort  : {0}", __box(myCompTrad->Compare(str1, str2)));
}

/*
This code produces the following output.

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

*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Comparer クラス | Comparer メンバ | System.Collections 名前空間 | IComparable | System.Globalization.CompareInfo | Thread.CurrentCulture | System.Globalization.CultureInfo