This documentation is archived and is not being maintained.

Array.Sort(Of T) Method (T(), Comparison(Of T))

Sorts the elements in an Array using the specified Comparison(Of T).

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

Public Shared Sub Sort(Of T) ( _
	array As T(), _
	comparison As Comparison(Of T) _

Type Parameters


The type of the elements of the array.


Type: T()
The one-dimensional, zero-based Array to sort
Type: System.Comparison(Of T)
The Comparison(Of T) to use when comparing elements.


array is Nothing.


comparison is Nothing.


The implementation of comparison caused an error during the sort. For example, comparison might not return 0 when comparing an item with itself.

If the sort is not successfully completed, the results are undefined.

This method uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

On average, this method is an O(n log n) operation, where n is the Length of array; in the worst case it is an O(n ^ 2) operation.

The following code example demonstrates 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 array 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
                ' 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 String = { _
            "Pachycephalosaurus", _
            "Amargasaurus", _
            "", _
            Nothing, _
            "Mamenchisaurus", _
            "Deinonychus" }

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

    End Sub

    Private Shared Sub Display(ByVal arr() As String)
        For Each s As String In arr
            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:

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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