C# Comparer Compare

Description

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

Syntax

Comparer.Compare has the following syntax.


public int Compare(
  Object a,
  Object b
)

Parameters

Comparer.Compare has the following parameters.

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

Returns

Comparer.Compare method returns A signed integer that indicates the relative values of a and b, as shown in the following table. Value Meaning Less than zero a is less than b. Zero a equals b. Greater than zero a is greater than b.

Example

The following code example shows how Compare returns different values depending on the culture associated with the Comparer.


using System;//from  w  w w. j  a  va2 s  .co m
using System.Collections;
using System.Globalization;

public class SamplesComparer  {

   public static void Main()  {
      String str1 = "bbb";
      String str2 = "aaa";
      Console.WriteLine(Comparer.DefaultInvariant.Compare( str1, str2 ) );
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack