Comparer.Compare(Object, Object) Comparer.Compare(Object, Object) Comparer.Compare(Object, Object) Comparer.Compare(Object, Object) Method

정의

대/소문자를 구분하여 같은 형식의 두 개체를 비교한 다음 한 개체가 다른 개체보다 작은지, 큰지 또는 두 개체가 같은지 여부를 나타내는 값을 반환합니다.Performs a case-sensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

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 Object Object Object

비교할 첫 번째 개체입니다.The first object to compare.

b
Object Object Object Object

비교할 두 번째 개체입니다.The second object to compare.

반환

다음 표와 같이 ab의 상대 값을 나타내는 부호 있는 정수입니다.A signed integer that indicates the relative values of a and b, as shown in the following table.

Value 의미Meaning
0보다 작음Less than zero ab보다 작은 경우a is less than b.
0Zero ab와 같습니다.a equals b.
0보다 큼Greater than zero ab보다 큰 경우a is greater than b.

구현

예외

a 또는 b 모두 IComparable 인터페이스를 구현하지 않는 경우Neither a nor b implements the IComparable interface.

또는-or- ab가 형식이 다르므로 서로 비교할 수 없는 경우a and b are of different types and neither one can handle comparisons with the other.

예제

다음 코드 예제에서는 어떻게 Compare 연결 된 문화권에 따라 다른 값을 반환 합니다 Comparer합니다.The following code example shows how Compare returns different values depending on the culture associated with the Comparer.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int 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 = gcnew Comparer( gcnew 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 = gcnew Comparer( gcnew 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

*/
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
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 'Main 

End Class 'SamplesComparer


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

설명

하는 경우 a 구현 IComparable, 다음 a합니다.If a implements IComparable, then a. CompareTo (b)를 반환 합니다; 그렇지 않고 b 구현 IComparable의 부정된 결과 b합니다.CompareTo (b) is returned; otherwise, if b implements IComparable, then the negated result of b. CompareTo (a)이 반환 됩니다.CompareTo (a) is returned.

비교 null 형식을 사용 하 여 허용 되 고 사용 하는 경우 예외를 생성 하지 않습니다 IComparable합니다.Comparing null with any type is allowed and does not generate an exception when using IComparable. 정렬할 때 null 다른 개체 보다 작은 것으로 간주 됩니다.When sorting, null is considered to be less than any other object.

문자열 비교는 문화권에 따라 다른 결과가 있을 수 있습니다.String comparisons might have different results depending on the culture. 문화권별 비교에 대 한 자세한 내용은 참조는 System.Globalization 네임 스페이스 및 전역화 및 지역화합니다.For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

적용 대상

추가 정보