Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Sort Method (Int32, Int32, IComparer)
Collapse the table of content
Expand the table of content

ArrayList.Sort Method (Int32, Int32, IComparer)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Overridable Sub Sort( _
   ByVal index As Integer, _
   ByVal count As Integer, _
   ByVal comparer As IComparer _
)
[C#]
public virtual void Sort(
 int index,
 int count,
 IComparer comparer
);
[C++]
public: virtual void Sort(
 int index,
 int count,
 IComparer* comparer
);
[JScript]
public function Sort(
   index : int,
 count : int,
 comparer : IComparer
);

Parameters

index
The zero-based starting index of the range to sort.
count
The length of the range to sort.
comparer
The IComparer implementation to use when comparing elements.

-or-

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

Exceptions

Exception Type Condition
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.

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

[Visual Basic, C#, C++] The following example shows how to sort the values in a section of an ArrayList using the default comparer and a custom comparer that reverses the sort order.

[Visual Basic] 
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 myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab + "{1}", i, myEnumerator.Current)
         i += 1
      End While
      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


[C#] 
using System;
using System.Collections;

public class SamplesArrayList  {

   public class myReverserClass : IComparer  {

      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
      int IComparer.Compare( Object x, Object y )  {
          return( (new CaseInsensitiveComparer()).Compare( y, x ) );
      }

   }

   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( "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, null );
      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.
      IComparer 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 );

   }
 
   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]:    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
*/


[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

__gc class myReverserClass : public IComparer  {
public:
    // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
    int Compare(Object* x, Object* y)  {
        return( (new CaseInsensitiveComparer())->Compare(y, x) );
    }
};

void PrintIndexAndValues(IEnumerable* myList)  {
    int i = 0;
    IEnumerator* myEnumerator = myList->GetEnumerator();
    while (myEnumerator->MoveNext())
        Console::WriteLine(S"\t[{0}]:\t{1}", (i++).ToString(), myEnumerator->Current->ToString());
    Console::WriteLine();
}

int main()  {
    // Creates and initializes a new ArrayList.
    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(S"The ArrayList initially contains the following values:");
    PrintIndexAndValues(myAL);

    // Sorts the values of the ArrayList using the default comparer.
    myAL->Sort(1, 3, 0);
    Console::WriteLine(S"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.
    IComparer* myComparer = new myReverserClass();
    myAL->Sort(1, 3, myComparer);
    Console::WriteLine(S"After sorting from index 1 to index 3 with the reverse case-insensitive comparer:");
    PrintIndexAndValues(myAL);
}

/* 
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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

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:
© 2015 Microsoft