ArrayList.Sort Method (Int32, Int32, IComparer)

Sorts the elements in a range of elements in ArrayList using the specified comparer.

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

'Declaration
Public Overridable Sub Sort ( _
	index As Integer, _
	count As Integer, _
	comparer As IComparer _
)

Parameters

index
Type: System.Int32

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

count
Type: System.Int32

The length of the range to sort.

comparer
Type: System.Collections.IComparer

The IComparer implementation to use when comparing elements.

-or-

A null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.

ExceptionCondition
ArgumentOutOfRangeException

index is less than zero.

-or-

count is less than zero.

ArgumentException

index and count do not specify a valid range in the ArrayList.

NotSupportedException

The ArrayList is read-only.

InvalidOperationException

An error occurred while comparing two elements.

If comparer is set to Nothing, this method performs a comparison sort (also called 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. To perform a stable sort, you must implement a custom IComparer interface.

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 shows how to sort the values in a range of elements in an ArrayList using the default comparer and a custom comparer that reverses the sort order.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList

   Public Class myReverserClass
      Implements IComparer

      ' Calls CaseInsensitiveComparer.Compare with the parameters reversed. 
      Public Function Compare( ByVal x As Object, ByVal y As Object) As Integer _
         Implements IComparer.Compare
         Return New CaseInsensitiveComparer().Compare(y, x)
      End Function 'IComparer.Compare

   End Class 'myReverserClass

   Public Shared Sub Main()

      ' Creates and initializes a new ArrayList. 
      Dim myAL As New ArrayList()
      myAL.Add("The")
      myAL.Add("QUICK")
      myAL.Add("BROWN")
      myAL.Add("FOX")
      myAL.Add("jumped")
      myAL.Add("over")
      myAL.Add("the")
      myAL.Add("lazy")
      myAL.Add("dog")

      ' Displays the values of the ArrayList.
      Console.WriteLine("The ArrayList initially contains the following values:")
      PrintIndexAndValues(myAL)

      ' Sorts the values of the ArrayList using the default comparer.
      myAL.Sort(1, 3, Nothing)
      Console.WriteLine("After sorting from index 1 to index 3 with the default comparer:")
      PrintIndexAndValues(myAL)

      ' Sorts the values of the ArrayList using the reverse case-insensitive comparer. 
      Dim myComparer = New myReverserClass()
      myAL.Sort(1, 3, myComparer)
      Console.WriteLine("After sorting from index 1 to index 3 with the reverse case-insensitive comparer:")
      PrintIndexAndValues(myAL)

   End Sub 'Main

   Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
      Dim i As Integer = 0
      Dim obj As [Object]
      For Each obj In  myList
         Console.WriteLine(vbTab + "[{0}]:" + vbTab + "{1}", i, obj)
         i = i + 1
      Next obj
      Console.WriteLine()
   End Sub 'PrintIndexAndValues

End Class 'SamplesArrayList 


'This code produces the following output. 
'The ArrayList initially contains the following values: 
'        [0]:    The 
'        [1]:    QUICK 
'        [2]:    BROWN 
'        [3]:    FOX 
'        [4]:    jumped 
'        [5]:    over 
'        [6]:    the 
'        [7]:    lazy 
'        [8]:    dog 

'After sorting from index 1 to index 3 with the default comparer: 
'        [0]:    The 
'        [1]:    BROWN 
'        [2]:    FOX 
'        [3]:    QUICK 
'        [4]:    jumped 
'        [5]:    over 
'        [6]:    the 
'        [7]:    lazy 
'        [8]:    dog 

'After sorting from index 1 to index 3 with the reverse case-insensitive comparer: 
'        [0]:    The 
'        [1]:    QUICK 
'        [2]:    FOX 
'        [3]:    BROWN 
'        [4]:    jumped 
'        [5]:    over 
'        [6]:    the 
'        [7]:    lazy 
'        [8]:    dog

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft