Export (0) Print
Expand All

Comparer.Compare 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.

[Visual Basic]
Public Overridable Function Compare( _
   ByVal a As Object, _
   ByVal b As Object _
) As Integer Implements IComparer.Compare
[C#]
public virtual int Compare(
 object a,
 object b
);
[C++]
public: virtual int Compare(
 Object* a,
 Object* b
);
[JScript]
public function Compare(
   a : Object,
 b : Object
) : int;

Parameters

a
The first object to compare.
b
The second object to compare.

Return Value

Value Condition
Less than zero a is less than b.
Zero a equals b.
Greater than zero a is greater than b.

Implements

IComparer.Compare

Exceptions

Exception Type Condition
ArgumentException Neither a nor b implements the IComparable interface.

-or-

a and b are of different types and neither one can handle comparisons with the other.

Remarks

If a implements IComparable, then a. CompareTo (b) is returned; otherwise, if b implements IComparable, then the negated result of b. CompareTo (a) is returned.

Comparing a null reference (Nothing in Visual Basic) with any type is allowed and does not generate an exception when using IComparable. When sorting, a null reference (Nothing) is considered to be less than any other object.

String comparisons might have different results depending on the culture. For more information on culture-specific comparisons, see the System.Globalization namespace and Developing World-Ready Applications.

Example

[Visual Basic, C#, C++] The following code example shows how Compare returns different values depending on the culture associated with the Comparer.

[Visual Basic] 
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


[C#] 
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

*/

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

int main()  {

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

    // Uses the DefaultInvariant Comparer.
    Console::WriteLine(S"   Invariant Comparer: {0}", __box(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(S"   International Sort: {0}", __box(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(S"   Traditional Sort  : {0}", __box(myCompTrad->Compare(str1, str2)));
}

/*
This code produces the following output.

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

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Comparer Class | Comparer Members | System.Collections Namespace | IComparable | System.Globalization.CompareInfo | Thread.CurrentCulture | System.Globalization.CultureInfo

Show:
© 2014 Microsoft