Export (0) Print
Expand All

ArrayList.Sort Method ()

.NET Framework 1.1

Sorts the elements in the entire ArrayList using the IComparable implementation of each element.

[Visual Basic]
Overloads Public Overridable Sub Sort()
[C#]
public virtual void Sort();
[C++]
public: virtual void Sort();
[JScript]
public function Sort();

Exceptions

Exception Type Condition
NotSupportedException The ArrayList is read-only.

Remarks

This method uses Array.Sort, which uses the QuickSort algorithm. This is an O(n ^2) operation, where n is the number of elements to sort, with an average of θ(n log n).

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.

Example

The following example shows how to sort the values in an ArrayList.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    
    
    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("jumps")
        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.
        myAL.Sort()
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("After sorting:")
        PrintIndexAndValues(myAL)
    End Sub 'Main
    
    
    Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
        Dim i As Integer = 0
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myEnumerator.Current)
            i = i + 1
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The ArrayList initially contains the following values:
'     [0]:    The
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
' 
' After sorting:
'     [0]:    brown
'     [1]:    dog
'     [2]:    fox
'     [3]:    jumps
'     [4]:    lazy
'     [5]:    over
'     [6]:    quick
'     [7]:    the
'     [8]:    The 

[C#] 
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      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.
      myAL.Sort();

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

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The ArrayList initially contains the following values:
    [0]:    The
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog

After sorting:
    [0]:    brown
    [1]:    dog
    [2]:    fox
    [3]:    jumps
    [4]:    lazy
    [5]:    over
    [6]:    quick
    [7]:    the
    [8]:    The
*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintIndexAndValues( IEnumerable* myList );
 
int main()  {
 
       // Creates and initializes a new ArrayList instance.
       ArrayList* myAL = new ArrayList();
       myAL->Add( S"The" );
       myAL->Add( S"quick" );
       myAL->Add( S"brown" );
       myAL->Add( S"fox" );
       myAL->Add( S"jumped" );
       myAL->Add( S"over" );
       myAL->Add( S"the" );
       myAL->Add( S"lazy" );
       myAL->Add( S"dog" );
 
       // Displays the values of the ArrayList.
       Console::WriteLine( "The ArrayList instance initially contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Sorts the values of the ArrayList.
       myAL->Sort();
 
       // Displays the values of the ArrayList.
       Console::WriteLine( "After sorting:" );
       PrintIndexAndValues( myAL );
    }
 
void PrintIndexAndValues( IEnumerable* myList )  {
       int i = 0;
       System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
       while ( myEnumerator->MoveNext() )
          Console::WriteLine( "\t[{0}]:\t{1}", __box(i++), myEnumerator->Current );
       Console::WriteLine();
    }

 /* 
 This code produces the following output.
 
The ArrayList instance 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

 */

[JScript] 
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.
myAL.Sort();

// 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 );
   Console.WriteLine();
}
 /* 
 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
 */

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace | ArrayList.Sort Overload List | Performing Culture-Insensitive String Operations in Collections

Show:
© 2014 Microsoft