Comparison(Of T) Delegate
Represents the method that compares two objects of the same type.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Delegate Function Comparison(Of T) ( _ x As T, _ y As T _ ) As Integer 'Usage Dim instance As New Comparison(Of T)(AddressOf HandlerMethod)
Type Parameters
- T
The type of the objects to compare.
Parameters
- x
- Type: T
The first object to compare.
- y
- Type: T
The second object to compare.
Return Value
Type: System.Int32Value | 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(Of T)(T(), Comparison(Of T)) method overload of the Array class and the Sort(Comparison(Of T)) method overload of the List(Of T) class to sort the elements of an array or list.
The following code example demonstrates the use of the Comparison(Of T) delegate with the Sort(Comparison(Of 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 Nothing, 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 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(Of T) 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 Else ' If x is Nothing and y is not Nothing, y ' is greater. Return -1 End If Else ' If x is not Nothing... ' If y Is Nothing Then ' ...and y is Nothing, x is greater. Return 1 Else ' ...and y is not Nothing, compare the ' lengths of the two strings. ' Dim retval As Integer = _ x.Length.CompareTo(y.Length) If retval <> 0 Then ' 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) End If End If End If End Function Public Shared Sub Main() Dim dinosaurs As New List(Of String) dinosaurs.Add("Pachycephalosaurus") dinosaurs.Add("Amargasaurus") dinosaurs.Add("") dinosaurs.Add(Nothing) dinosaurs.Add("Mamenchisaurus") dinosaurs.Add("Deinonychus") Display(dinosaurs) Console.WriteLine(vbLf & "Sort with generic Comparison(Of String) delegate:") dinosaurs.Sort(AddressOf CompareDinosByLength) Display(dinosaurs) End Sub Private Shared Sub Display(ByVal lis As List(Of String)) Console.WriteLine() For Each s As String In lis If s Is Nothing Then Console.WriteLine("(Nothing)") Else Console.WriteLine("""{0}""", s) End If Next End Sub End Class ' This code example produces the following output: ' '"Pachycephalosaurus" '"Amargasaurus" '"" '(Nothing) '"Mamenchisaurus" '"Deinonychus" ' 'Sort with generic Comparison(Of String) delegate: ' '(Nothing) '"" '"Deinonychus" '"Amargasaurus" '"Mamenchisaurus" '"Pachycephalosaurus"
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.