Sorts the elements in an Array using the specified Comparison(Of T).
Imports System
Imports System.Collections.Generic
Public Class Example
Private Shared Function MyComparer(ByVal x As String, ByVal y As String) As Integer
If x Is Nothing Then
If y Is Nothing Then
Return 0
Else
Return -1
End If
Else
If y Is Nothing Then
Return 1
Else
Dim retval As Integer = x.Length.CompareTo(y.Length)
If retval <> 0 Then
Return retval
Else
Return x.CompareTo(y)
End If
End If
End If
End Function
Public Shared Sub Main()
Dim numbers() As String = {"Ten","Two","",Nothing,"Three","Four" }
Display(numbers)
Array.Sort(numbers, AddressOf MyComparer)
Display(numbers)
End Sub
Private Shared Sub Display(ByVal arr() As String)
Console.WriteLine()
For Each s As String In arr
If s Is Nothing Then
Console.WriteLine("(Nothing)")
Else
Console.WriteLine("""{0}""", s)
End If
Next
End Sub
End Class
Related examples in the same category