Comparer.Compare(Object, Object) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Voert een hoofdlettergevoelige vergelijking van twee objecten van hetzelfde type uit en retourneert een waarde die aangeeft of de ene kleiner is dan, gelijk is aan of groter is dan de andere.
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
Parameters
- a
- Object
Het eerste object dat moet worden vergeleken.
- b
- Object
Het tweede object dat moet worden vergeleken.
Retouren
Een ondertekend geheel getal dat de relatieve waarden van a en b, zoals wordt weergegeven in de volgende tabel, aangeeft.
| Waarde | Betekenis |
|---|---|
| Kleiner dan nul |
a is kleiner dan b.
|
| Nul |
a is bgelijk aan .
|
| Groter dan nul |
a is groter dan b.
|
Implementeringen
Uitzonderingen
b Noch a implementeert u de IComparable interface.
– of –
a en zijn van verschillende typen en b geen van beide kan vergelijkingen met de andere verwerken.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe Compare verschillende waarden worden geretourneerd, afhankelijk van de cultuur die aan de Comparercultuur is gekoppeld.
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
Opmerkingen
Als a dit wordt geïmplementeerd IComparable, dan a.
CompareTo (b) wordt geretourneerd; anders, indien b geïmplementeerd IComparable, wordt het negatieve resultaat van b.
CompareTo (a) wordt geretourneerd.
Vergelijking null met elk type is toegestaan en genereert geen uitzondering bij gebruik IComparable. Bij het sorteren null wordt beschouwd als kleiner dan elk ander object.
Tekenreeksvergelijkingen kunnen verschillende resultaten hebben, afhankelijk van de cultuur. Zie de System.Globalization naamruimte en globalisatie en lokalisatie voor meer informatie over cultuurspecifieke vergelijkingen.