Export (0) Print
Expand All
Expand Minimize

Comparison<T> Delegate

Represents the method that compares two objects of the same type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public delegate int Comparison<in T>(
	T x,
	T y
)

Type Parameters

in T

The type of the objects to compare.

This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see 2678dc63-c7f9-4590-9ddc-0a4df684d42e.

Parameters

x
Type: T
The first object to compare.
y
Type: T
The second object to compare.

Return Value

Type: System.Int32
A signed integer that indicates the relative values of x and y, as shown in the following table.

Value

Condition

Less than 0

x is less than y.

0

x equals y.

Greater than 0

x is greater than y.

This delegate is used by the Sort<T>(T[], Comparison<T>) method overload of the Array class and the Sort(Comparison<T>) method overload of the List<T> class to sort the elements of an array or list.

The following code example demonstrates the use of the Comparison<T> delegate with the Sort(Comparison<T>) method overload.

The code example defines an alternative comparison method for strings, named CompareDinosByLength. This method works as follows: First, the comparands are tested for null, and a null reference is treated as less than a non-null. Second, the string lengths are compared, and the longer string is deemed to be greater. Third, if the lengths are equal, ordinary string comparison is used.

A List<T> of strings is created and populated with four strings, in no particular order. The list also includes an empty string and a null reference. The list is displayed, sorted using a Comparison<T> generic delegate representing the CompareDinosByLength method, and displayed again.


using System;
using System.Collections.Generic;

public class Example
{
   private static int CompareDinosByLength(string x, string y)
   {
      if (x == null)
      {
         if (y == null)
         {
            // If x is null and y is null, they're
            // equal. 
            return 0;
         }
         else
         {
            // If x is null and y is not null, y
            // is greater. 
            return -1;
         }
      }
      else
      {
         // If x is not null...
         //
         if (y == null)
         // ...and y is null, x is greater.
         {
            return 1;
         }
         else
         {
            // ...and y is not null, compare the 
            // lengths of the two strings.
            //
            int retval = x.Length.CompareTo(y.Length);

            if (retval != 0)
            {
               // If the strings are not of equal length,
               // the longer string is greater.
               //
               return retval;
            }
            else
            {
               // If the strings are of equal length,
               // sort them with ordinary string comparison.
               //
               return x.CompareTo(y);
            }
         }
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      List<string> dinosaurs = new List<string>();
      dinosaurs.Add("Pachycephalosaurus");
      dinosaurs.Add("Amargasaurus");
      dinosaurs.Add("");
      dinosaurs.Add(null);
      dinosaurs.Add("Mamenchisaurus");
      dinosaurs.Add("Deinonychus");
      Display(outputBlock, dinosaurs);

      outputBlock.Text += "\nSort with generic Comparison<string> delegate:" + "\n";
      dinosaurs.Sort(CompareDinosByLength);
      Display(outputBlock, dinosaurs);

   }

   private static void Display(System.Windows.Controls.TextBlock outputBlock, List<string> list)
   {
      outputBlock.Text += "\n";
      foreach (string s in list)
      {
         if (s == null)
            outputBlock.Text += "(null)" + "\n";
         else
            outputBlock.Text += String.Format("\"{0}\"", s) + "\n";
      }
   }
}

/* This code example produces the following output:

"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"

Sort with generic Comparison<string> delegate:

(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft