Sorts the elements in an Array using the specified Comparison(Of T). : Array Sort « Data Structure « VB.Net






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

1.Array Reverse and SortArray Reverse and Sort
2.Array.Reverse(values) DemoArray.Reverse(values) Demo
3.Sort an Array and Use Array.Length propertiesSort an Array and Use Array.Length properties
4.Sort an ArraySort an Array
5.Reverse the order - elements will be in descending orderReverse the order - elements will be in descending order
6.Array.Sort<(Of <(TKey, TValue>)>)(array[]()[], array[]()[]),
7.Array.Sort sorts the elements in an entire one-dimensional Array using the IComparable
8.Sorts a section of the Array using the reverse case-insensitive comparer
9.Sorts the entire Array using the default comparer
10.Sorts the entire Array using the reverse case-insensitive comparer
11.Sorts a pair of one-dimensional Array objects
12.Sorts a section of the Array pair using the reverse case-insensitive comparer
13.Sorts the entire Array pair using the default comparer
14.Sorts the entire Array pair using the reverse case-insensitive comparer
15.Sorts the elements in a range in an Array using the IComparable(Of T) generic interface
16.Sorts a pair of Array objects (one contains the keys and the other contains the values)