IComparer.Compare(Object, Object) Metoda

Definicja

Porównuje dwa obiekty i zwraca wartość wskazującą, czy jedna z nich jest mniejsza, równa lub większa od drugiej.Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

public:
 int Compare(System::Object ^ x, System::Object ^ y);
public int Compare (object x, object y);
public int Compare (object? x, object? y);
abstract member Compare : obj * obj -> int
Public Function Compare (x As Object, y As Object) As Integer

Parametry

x
Object

Pierwszy obiekt do porównania.The first object to compare.

y
Object

Drugi obiekt do porównania.The second object to compare.

Zwraca

Int32

Liczba całkowita ze znakiem, która wskazuje wartości względne x i y :A signed integer that indicates the relative values of x and y:
-Jeśli jest mniejsza niż 0, x jest mniejsza niż y .- If less than 0, x is less than y.
-Jeśli 0, x równa się y .- If 0, x equals y.
-Jeśli większa niż 0, x jest większa niż y .- If greater than 0, x is greater than y.

Wyjątki

Ani x nie y implementuje IComparable interfejsu.Neither x nor y implements the IComparable interface.

-lub--or- x i y są różne typy, a żaden z nich nie może obsłużyć porównania z drugim.x and y are of different types and neither one can handle comparisons with the other.

Przykłady

Poniższy przykład używa IComparer interfejsu do sortowania tablicy ciągów.The following example uses the IComparer interface to sort a string array. W tym przykładzie Compare Metoda jest implementowana przy użyciu CaseInsensitiveComparer klasy w celu odwrócenia kolejności zawartości tablicy.In this example, the Compare method is implemented using the CaseInsensitiveComparer class to reverse the order of the contents of the array.

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

Uwagi

Preferowana implementacja polega na użyciu CompareTo metody jednego z parametrów.The preferred implementation is to use the CompareTo method of one of the parameters.

Porównywanie null z dowolnym typem jest dozwolone i nie generuje wyjątku podczas korzystania z programu IComparable .Comparing null with any type is allowed and does not generate an exception when using IComparable. Podczas sortowania null jest uznawana za mniej niż każdy inny obiekt.When sorting, null is considered to be less than any other object.

Dotyczy

Zobacz też