Comparer.Compare(Object, Object) Méthode

Définition

Exécute une comparaison avec respect de la casse de deux objets du même type et retourne une valeur indiquant si l'un est inférieur, égal ou supérieur à l'autre.

public:
 virtual int Compare(System::Object ^ a, System::Object ^ b);
public int Compare (object a, 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

Paramètres

a
Object

Premier objet à comparer.

b
Object

Deuxième objet à comparer.

Retours

Entier signé qui indique les valeurs relatives de a et b, comme indiqué dans le tableau suivant.

Value Signification
Inférieure à zéroa est inférieur à b.
Zéroa est égal à b.
Supérieure à zéroa est supérieur à b.

Implémente

Exceptions

Ni a ni b n'implémente l'interface IComparable.

- ou -

a et b sont de types différents et aucun d'entre eux ne peut effectuer de comparaisons avec l'autre.

Exemples

L’exemple de code suivant montre comment Compare retourne différentes valeurs en fonction de la culture associée au 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.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

Remarques

Si a implémente IComparable, alors a. CompareTo (b) est retourné ; sinon, si b implémente IComparable, le résultat annulé de b. CompareTo (a) est retourné.

La comparaison null avec n’importe quel type est autorisée et ne génère pas d’exception lors de l’utilisation de IComparable. Lors du tri, null est considéré comme inférieur à tout autre objet.

Les comparaisons de chaînes peuvent avoir des résultats différents en fonction de la culture. Pour plus d’informations sur les comparaisons spécifiques à la culture, consultez espace System.Globalization de noms et Globalisation et Localisation.

S’applique à

Voir aussi