Comparer.Compare(Object, Object) 메서드

정의

같은 형식의 두 개체에 대해 대/소문자를 구분하여 비교하고 한 개체가 다른 개체보다 작거나 같은지 또는 다른 개체보다 큰지를 나타내는 값을 반환합니다.

public:
 virtual int Compare(System::Object ^ a, System::Object ^ b);
public int Compare(object a, object b);
abstract member Compare : obj * obj -> int
override this.Compare : obj * obj -> int
Public Function Compare (a As Object, b As Object) As Integer

매개 변수

a
Object

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

b
Object

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

반품

다음 표와 같이 상대 값을 a 나타내는 부가 정수입니다 b.

의미
0보다 작음 a 가 .보다 b작습니다.
0 a같음 .b
0보다 큼 ab보다 큽니다.

구현

예외

a 인터페이스도 구현 IComparable 하지 않습니다b.

-또는-

a 형식 b 이 다르고 둘 다 다른 형식과의 비교를 처리할 수 없습니다.

예제

다음 코드 예제에서는 연결된 문화권에 따라 다른 값을 반환하는 방법을 Compare 보여 있습니다 Comparer.

using System;
using System.Collections;
using System.Globalization;

public class SamplesComparer  {

   public static void Main()  {

      // Creates the strings to compare.
      String str1 = "llegar";
      String str2 = "lugar";
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );

      // Uses the DefaultInvariant Comparer.
      Console.WriteLine( "   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Comparer myCompIntl = new Comparer( new CultureInfo( "es-ES", false ) );
      Console.WriteLine( "   International Sort: {0}", myCompIntl.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Comparer myCompTrad = new Comparer( new CultureInfo( 0x040A, false ) );
      Console.WriteLine( "   Traditional Sort  : {0}", myCompTrad.Compare( str1, str2 ) );
   }
}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
Imports System.Collections
Imports System.Globalization

Public Class SamplesComparer

   Public Shared Sub Main()

      ' Creates the strings to compare.
      Dim str1 As [String] = "llegar"
      Dim str2 As [String] = "lugar"
      Console.WriteLine("Comparing ""{0}"" and ""{1}"" ...", str1, str2)

      ' Uses the DefaultInvariant Comparer.
      Console.WriteLine("   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare(str1, str2))

      ' Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Dim myCompIntl As New Comparer(New CultureInfo("es-ES", False))
      Console.WriteLine("   International Sort: {0}", myCompIntl.Compare(str1, str2))

      ' Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Dim myCompTrad As New Comparer(New CultureInfo(&H40A, False))
      Console.WriteLine("   Traditional Sort  : {0}", myCompTrad.Compare(str1, str2))

   End Sub

End Class


'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
'   Invariant Comparer: -1
'   International Sort: -1
'   Traditional Sort  : 1

설명

를 구현하는 경우 a .aIComparable CompareTo (b)가 반환되고, b 그렇지 않으면 구현되면 IComparable부정된 결과가 반환 b됩니다. CompareTo (a)가 반환됩니다.

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

문자열 비교는 문화권에 따라 다른 결과를 가질 수 있습니다. 문화권별 비교에 대한 자세한 내용은 네임스페이 System.Globalization 스, 세계화 및 지역화를 참조하세요.

적용 대상

추가 정보