Comparison Generic Delegate

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

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

Public Delegate Function Comparison(Of T) ( _
	x As T, _
	y As T _
) As Integer
Dim instance As New Comparison(Of T)(AddressOf HandlerMethod)
J# supports the use of generic types and methods, but not the declaration of new ones.
JScript does not support generic types and methods.
Not applicable.

Type Parameters


The type of the objects to compare.



The first object to compare.


The second object to compare.

Return Value



Less than 0

x is less than y.


x equals y.

Greater than 0

x is greater than y.

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

The following code example demonstrates the use of the Comparison delegate with the Sort 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 a null reference (Nothing in Visual Basic), 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 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 generic delegate representing the CompareDinosByLength method, and displayed again.

Imports System
Imports System.Collections.Generic

Public Class Example

    Private Shared Function CompareDinosByLength( _
        ByVal x As String, ByVal y As String) As Integer

        If x Is Nothing Then
            If y Is Nothing Then 
                ' If x is Nothing and y is Nothing, they're
                ' equal. 
                Return 0
                ' If x is Nothing and y is not Nothing, y
                ' is greater. 
                Return -1
            End If
            ' If x is not Nothing...
            If y Is Nothing Then
                ' ...and y is Nothing, x is greater.
                Return 1
                ' ...and y is not Nothing, compare the 
                ' lengths of the two strings.
                Dim retval As Integer = _

                If retval <> 0 Then 
                    ' If the strings are not of equal length,
                    ' the longer string is greater.
                    Return retval
                    ' If the strings are of equal length,
                    ' sort them with ordinary string comparison.
                    Return x.CompareTo(y)
                End If
            End If
        End If

    End Function

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        Console.WriteLine(vbLf & "Sort with generic Comparison(Of String) delegate:")
        dinosaurs.Sort(AddressOf CompareDinosByLength)

    End Sub

    Private Shared Sub Display(ByVal lis As List(Of String))
        For Each s As String In lis
            If s Is Nothing Then
                Console.WriteLine("""{0}""", s)
            End If
    End Sub
End Class

' This code example produces the following output:
'Sort with generic Comparison(Of String) delegate:

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0