IComparer 接口

定义

提供比较两个对象的方法。

public interface class IComparer
public interface IComparer
[System.Runtime.InteropServices.ComVisible(true)]
public interface IComparer
type IComparer = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type IComparer = interface
Public Interface IComparer
派生
属性

示例

以下示例使用 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
Imports System.Collections

Public Class Example
   Public Class ReverserClass : Implements IComparer
      ' Call CaseInsensitiveComparer.Compare with the parameters reversed.
      Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
             Implements IComparer.Compare
         Return New CaseInsensitiveComparer().Compare(y, x)
      End Function 
   End Class

   Public Shared Sub Main()
      ' Initialize a string array.
      Dim words() As String = { "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 of the ArrayList 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)
   End Sub 

   Public Shared Sub PrintIndexAndValues(list As IEnumerable)
      Dim i As Integer = 0
      For Each item In  list
         Console.WriteLine($"   [{i}]:  {item}")
         i += 1
      Next
      Console.WriteLine()
   End Sub 
End Class
' 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

注解

此接口与 Array.SortArray.BinarySearch 方法结合使用。 它提供了一种自定义集合的排序顺序的方法。 有关参数和返回值的注释, Compare 请参阅 方法。 其泛型等效项是 System.Collections.Generic.IComparer<T> 接口。

此接口的默认实现是 Comparer 类。 有关此接口的通用版本,请参阅 System.Collections.Generic.IComparer<T>

方法

Compare(Object, Object)

比较两个对象并返回一个值,该值指示一个对象小于、等于还是大于另一个对象。

适用于

另请参阅