Expand Minimize

Comparison<T> Delegate

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

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

generic<typename T>
public delegate int Comparison(
	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 Covariance and Contravariance in Generics.

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

Meaning

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>(array<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 nullptr, 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 namespace System;
using namespace System::Collections::Generic;

int CompareDinosByLength(String^ x, String^ y)
{
    if (x == nullptr)
    {
        if (y == nullptr)
        {
            // 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 == nullptr)
            // ...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);
            }
        }
    }
};

void Display(List<String^>^ list)
{
    Console::WriteLine();
    for each(String^ s in list)
    {
        if (s == nullptr)
            Console::WriteLine("(null)");
        else
            Console::WriteLine("\"{0}\"", s);
    }
};

void main()
{
    List<String^>^ dinosaurs = gcnew List<String^>();
    dinosaurs->Add("Pachycephalosaurus");
    dinosaurs->Add("Amargasaurus");
    dinosaurs->Add("");
    dinosaurs->Add(nullptr);
    dinosaurs->Add("Mamenchisaurus");
    dinosaurs->Add("Deinonychus");
    Display(dinosaurs);

    Console::WriteLine("\nSort with generic Comparison<String^> delegate:");
    dinosaurs->Sort(
        gcnew Comparison<String^>(CompareDinosByLength));
    Display(dinosaurs);

}

/* This code example produces the following output:

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

Sort with generic Comparison<String^> delegate:

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

The following example uses the Comparison<T> delegate to sort the elements of a collection of CityInfo objects. CityInfo is an application-defined class that contains information about a city and its population. The example defines three methods, CompareByName, CompareByPopulation, and CompareByNames, that offer three different ways of ordering the CityInfo objects. Each method is assigned to the comparison argument of the Array::Sort<T>(array<T>, Comparison<T>) method.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft