Comparer.Compare 方法

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

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function Compare ( _
    a As Object, _
    b As Object _
) As Integer
用法
Dim instance As Comparer
Dim a As Object
Dim b As Object
Dim returnValue As Integer

returnValue = instance.Compare(a, b)
public int Compare (
    Object a,
    Object b
)
public:
virtual int Compare (
    Object^ a, 
    Object^ b
) sealed
public final int Compare (
    Object a, 
    Object b
)
public final function Compare (
    a : Object, 
    b : Object
) : int

参数

  • a
    要比较的第一个对象。
  • b
    要比较的第二个对象。

返回值

条件

小于零

a 小于 b。

a 等于 b。

大于零

a 大于 b。

异常

异常类型 条件

ArgumentException

a 和 b 都不实现 IComparable 接口。

- 或 -

a 和 b 的类型不同,而且都无法处理与另一个的比较。

备注

如果 a 实现 IComparable,则 a。返回 CompareTo ( b );否则,如果 b 实现 IComparable,则返回负值的 b 结果。返回 CompareTo (a)。

当使用 IComparable 时,允许将 空引用(在 Visual Basic 中为 Nothing) 与任何类型进行比较,而且不会生成异常。排序时,空引用(在 Visual Basic 中为 Nothing) 被视为小于任何其他对象。

根据区域性的不同,字符串比较可能会有不同的结果。有关区域性特定比较的更多信息,请参见 System.Globalization 命名空间和 编码和本地化

示例

下面的代码示例显示 Compare 是如何根据与 Comparer 关联的区域性返回不同的值的。

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

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

*/
import System.* ;
import System.Collections.*;
import System.Globalization.*;

public class SamplesComparer
{
    public static void main(String[] args)
    {
        // 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}", 
            (Int32)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}", 
            (Int32)myCompIntl.Compare(str1, str2));

        // Uses the Comparer based on the culture identifier 0x040A 
        // (Spanish - Spain, traditional sort).
        Comparer myCompTrad = new Comparer(new CultureInfo(0x40A, false));
        Console.WriteLine("   Traditional Sort  : {0}",
            (Int32)myCompTrad.Compare(str1, str2));
    } //main
} //SamplesComparer

/*
This code produces the following output.

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

*/

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Comparer 类
Comparer 成员
System.Collections 命名空间
IComparable 接口
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo