IComparer.Compare(Object, Object) 메서드

정의

두 개체를 비교하여 한 개체가 다른 개체보다 작거나, 같거나 또는 크다는 것을 나타내는 값을 반환합니다.

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

매개 변수

x
Object

비교할 첫 번째 개체입니다.

y
Object

비교할 두 번째 개체입니다.

반환

xy의 상대 값을 나타내는 부호 있는 정수입니다.
- 0 보다 작은 경우 xy보다 작습니다.
- 0인 경우 xy와 같습니다.
- 0보다 큰 경우 xy보다 큽니다.

예외

x 또는 y 모두 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
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

설명

기본 구현은 매개 변수 중 하나의 메서드를 사용하는 CompareTo 것입니다.

모든 형식과 비교하면 null 허용되며 를 사용할 IComparable때 예외가 생성되지 않습니다. 정렬할 null 때 는 다른 개체보다 작은 것으로 간주됩니다.

적용 대상

추가 정보