IComparer.Compare(Object, Object) Methode

Definition

Vergleicht zwei Objekte und gibt einen Wert zurück, der angibt, ob ein Wert niedriger, gleich oder größer als der andere Wert ist.

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

Parameter

x
Object

Das erste zu vergleichende Objekt.

y
Object

Das zweite zu vergleichende Objekt.

Gibt zurück

Eine ganze Zahl mit Vorzeichen, die die relativen Werte von x und y angibt:
– Wenn diese kleiner als 0 (null) sind, ist x kleiner als y.
– Wenn diese 0 (null) sind, ist x gleich y.
– Wenn diese größer als 0 sind, ist x größer als y.

Ausnahmen

Weder x noch y implementieren die IComparable-Schnittstelle.

- oder -

x und y sind nicht vom gleichen Typ. Keines der beiden kann einen Vergleich mit dem jeweils anderen Objekt behandeln.

Beispiele

Im folgenden Beispiel wird die IComparer Schnittstelle verwendet, um ein Zeichenfolgenarray zu sortieren. In diesem Beispiel wird die Compare -Methode mithilfe der CaseInsensitiveComparer -Klasse implementiert, um die Reihenfolge des Inhalts des Arrays umzukehren.

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

Hinweise

Die bevorzugte Implementierung besteht darin, die CompareTo Methode eines der Parameter zu verwenden.

Der Vergleich null mit einem beliebigen Typ ist zulässig und generiert keine Ausnahme, wenn verwendet IComparablewird. Wird beim Sortieren null als kleiner betrachtet als jedes andere Objekt.

Gilt für:

Weitere Informationen