ArrayList.Sort Method

Updated: October 2008

Sorts the elements in the entire ArrayList.

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

public function Sort()


The ArrayList is read-only.

This method uses Array.Sort, which uses the QuickSort algorithm. The QuickStort algorithm is a comparison sort (also called an unstable sort), which means that a "less than or equal to" comparison operation determines which of two elements should occur first in the final sorted list. However, if two elements are equal, their original 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 to use with the other overloads of this method.

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 an ArrayList.

import System;
import System.Collections;

// Creates and initializes a new ArrayList.
var myAL : ArrayList = 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.

// Displays the values of the ArrayList.
Console.WriteLine( "After sorting:" );
PrintIndexAndValues( myAL );

function PrintIndexAndValues( myList : IEnumerable )  {
   var i : int = 0;
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
 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:
     [0]:    brown
     [1]:    dog
     [2]:    fox
     [3]:    jumped
     [4]:    lazy
     [5]:    over
     [6]:    quick
     [7]:    the
     [8]:    The

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, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0




October 2008

Revised the Remarks section to note that the QuickSort algorithm is a comparison sort.

Information enhancement.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft