英語で読む

次の方法で共有


IComparer.Compare(Object, Object) メソッド

定義

2 つのオブジェクトを比較して、一方が他方より小さいか、同じか、または大きいかを示す値を返します。

public int Compare (object x, object y);
public int Compare (object? x, object? y);

パラメーター

x
Object

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

y
Object

比較する 2 番目のオブジェクト。

戻り値

xy の相対値を示す符号付き整数。
- 0 より小さい場合、xy よりも小さくなります。
- 0 の場合、xy と等しくなります。
- 0 より大きい場合、xy よりも大きくなります。

例外

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

- または -

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

次の例では、 インターフェイスを IComparer 使用して文字列配列を並べ替えます。 この例では、 クラスを Compare 使用して メソッドを CaseInsensitiveComparer 実装し、配列の内容の順序を逆にします。

using System;
using System.Collections;

public class Example
{
   public class ReverserClass : IComparer
   {
      // Call CaseInsensitiveComparer.Compare with the parameters reversed.
      int IComparer.Compare(Object x, Object y)
      {
          return ((new CaseInsensitiveComparer()).Compare(y, x));
      }
   }

   public static void Main()
   {
      // Initialize a string array.
      string[] words = { "The", "quick", "brown", "fox", "jumps", "over",
                         "the", "lazy", "dog" };

      // Display the array values.
      Console.WriteLine("The array initially contains the following values:" );
      PrintIndexAndValues(words);

      // Sort the array values using the default comparer.
      Array.Sort(words);
      Console.WriteLine("After sorting with the default comparer:" );
      PrintIndexAndValues(words);

      // Sort the array values using the reverse case-insensitive comparer.
      Array.Sort(words, new ReverserClass());
      Console.WriteLine("After sorting with the reverse case-insensitive comparer:");
      PrintIndexAndValues(words);
   }

   public static void PrintIndexAndValues(IEnumerable list)
   {
      int i = 0;
      foreach (var item in list )
         Console.WriteLine($"   [{i++}]:  {item}");

      Console.WriteLine();
   }
}
// The example displays the following output:
//       The array initially contains the following values:
//          [0]:  The
//          [1]:  quick
//          [2]:  brown
//          [3]:  fox
//          [4]:  jumps
//          [5]:  over
//          [6]:  the
//          [7]:  lazy
//          [8]:  dog
//
//       After sorting with the default comparer:
//          [0]:  brown
//          [1]:  dog
//          [2]:  fox
//          [3]:  jumps
//          [4]:  lazy
//          [5]:  over
//          [6]:  quick
//          [7]:  the
//          [8]:  The
//
//       After sorting with the reverse case-insensitive comparer:
//          [0]:  the
//          [1]:  The
//          [2]:  quick
//          [3]:  over
//          [4]:  lazy
//          [5]:  jumps
//          [6]:  fox
//          [7]:  dog
//          [8]:  brown

注釈

推奨される実装は、いずれかのパラメーターの メソッドを使用 CompareTo することです。

任意の null 型との比較が許可され、 を使用 IComparableする場合は例外は生成されません。 並べ替えると、 null は他のどのオブジェクトよりも小さいと見なされます。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください