语言

Comparer.Compare(Object, Object) 方法

定义

对同一类型的两个对象执行区分大小写的比较,并返回一个值,该值指示一个对象是小于、等于还是大于另一个对象。

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

参数

a
Object

要比较的第一个对象。

b
Object

要比较的第二个对象。

返回

一个有符号整数,指示其相对值 a , b如下表所示。

值 意义
小于零 a 小于 b。
零 a 等于 b。
大于零 a 大于 b。

实现

例外

既ab不实现接口,也不实现IComparable接口。

-或-

a 且 b 类型不同,两者都不能处理与其他类型的比较。

示例

下面的代码示例演示如何根据与 <a0/&a0> 关联的区域性返回不同的值。

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 实现 IComparable,则 a为 .. CompareTo (b) 返回;否则,如果 b 实现 IComparable,则为否定结果 b。 CompareTo (a) 返回。

与 null 任何类型进行比较是允许的,在使用时 IComparable不会生成异常。 排序时, null 被视为小于任何其他对象。

字符串比较可能具有不同的结果,具体取决于区域性。 有关区域性特定比较的详细信息,请参阅 System.Globalization 命名空间 和全球化和本地化。

适用于

另请参阅