This documentation is archived and is not being maintained.

List(Of T).Sort Method (Int32, Int32, IComparer(Of T))

Sorts the elements in a range of elements in List(Of T) using the specified comparer.

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

Public Sub Sort ( _
	index As Integer, _
	count As Integer, _
	comparer As IComparer(Of T) _
Dim instance As List 
Dim index As Integer 
Dim count As Integer 
Dim comparer As IComparer(Of T)

instance.Sort(index, count, comparer)


Type: System.Int32

The zero-based starting index of the range to sort.

Type: System.Int32

The length of the range to sort.

Type: System.Collections.Generic.IComparer(Of T)

The IComparer(Of T) implementation to use when comparing elements, or Nothing to use the default comparer Comparer(Of T).Default.


index is less than 0.


count is less than 0.


index and count do not specify a valid range in the List(Of T).


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


comparer is Nothing, and the default comparer Comparer(Of T).Default cannot find implementation of the IComparable(Of T) generic interface or the IComparable interface for type T.

If comparer is provided, the elements of the List(Of T) are sorted using the specified IComparer(Of T) implementation.

If comparer is Nothing, the default comparer Comparer(Of T).Default checks whether type T implements the IComparable(Of T) generic interface and uses that implementation, if available. If not, Comparer(Of T).Default checks whether type T implements the IComparable interface. If type T does not implement either interface, Comparer(Of T).Default throws an InvalidOperationException.

This method uses Array.Sort, which 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 Count; in the worst case it is an O(n ^ 2) operation.

The following code example demonstrates the Sort(Int32, Int32, IComparer(Of T)) method overload and the BinarySearch(Int32, Int32, T, IComparer(Of T)) method overload.

The code example defines an alternative comparer for strings named DinoCompare, which implements the IComparer<string> (IComparer(Of String) in Visual Basic, IComparer<String^> in Visual C++) generic interface. The comparer 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 the names of five herbivorous dinosaurs and three carnivorous dinosaurs. Within each of the two groups, the names are not in any particular sort order. The list is displayed, the range of herbivores is sorted using the alternate comparer, and the list is displayed again.

The BinarySearch(Int32, Int32, T, IComparer(Of T)) method overload is then used to search only the range of herbivores for "Brachiosaurus". The string is not found, and the bitwise complement (the ~ operator in C# and Visual C++, Xor -1 in Visual Basic) of the negative number returned by the BinarySearch(Int32, Int32, T, IComparer(Of T)) method is used as an index for inserting the new string.

Imports System
Imports System.Collections.Generic

Public Class DinoComparer
    Implements IComparer(Of String)

    Public Function Compare(ByVal x As String, _
        ByVal y As String) As Integer _
        Implements IComparer(Of String).Compare

        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 
End Class 

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)


        Dim herbivores As Integer = 5

        Dim dc As New DinoComparer

        Console.WriteLine(vbLf & _
            "Sort a range with the alternate comparer:")
        dinosaurs.Sort(0, herbivores, dc)

        Console.WriteLine(vbLf & _
            "BinarySearch a range and Insert ""{0}"":", _

        Dim index As Integer = _
            dinosaurs.BinarySearch(0, herbivores, "Brachiosaurus", dc)

        If index < 0 Then
            index = index Xor -1
            dinosaurs.Insert(index, "Brachiosaurus")
            herbivores += 1
        End If


    End Sub 

    Private Shared Sub Display(ByVal lis As List(Of String))
        For Each s As String In lis
    End Sub 
End Class 

' This code example produces the following output: 


'Sort a range with the alternate comparer: 


'BinarySearch a range and Insert "Brachiosaurus": 


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.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0