# ArrayList.SortArrayList.SortArrayList.SortArrayList.Sort Method

## 定义

ArrayList 或它的一部分中的元素进行排序。Sorts the elements in the ArrayList or a portion of it.

## 重载

 Sort() Sort() Sort() Sort() 对整个 ArrayList 中的元素进行排序。Sorts the elements in the entire ArrayList. Sort(IComparer) Sort(IComparer) Sort(IComparer) Sort(IComparer) 使用指定的比较器对整个 ArrayList 中的元素进行排序。Sorts the elements in the entire ArrayList using the specified comparer. Sort(Int32, Int32, IComparer) Sort(Int32, Int32, IComparer) Sort(Int32, Int32, IComparer) Sort(Int32, Int32, IComparer) 使用指定的比较器对 ArrayList 中某个范围内的元素进行排序。Sorts the elements in a range of elements in ArrayList using the specified comparer.

## Sort()Sort()Sort()Sort()

``````public:
virtual void Sort();``````
``public virtual void Sort ();``
``````abstract member Sort : unit -> unit
override this.Sort : unit -> unit``````
``Public Overridable Sub Sort ()``

### 示例

``````using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{

// Creates and initializes a new ArrayList.
ArrayList^ myAL = gcnew ArrayList;

// Displays the values of the ArrayList.
Console::WriteLine( "The ArrayList initially contains the following values:" );
PrintValues( myAL );

// Sorts the values of the ArrayList.
myAL->Sort();

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

void PrintValues( IEnumerable^ myList )
{
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( "   {0}", obj );
}

Console::WriteLine();
}

/*
This code produces the following output.

The ArrayList initially contains the following values:
The
quick
brown
fox
jumps
over
the
lazy
dog

After sorting:
brown
dog
fox
jumps
lazy
over
quick
the
The
*/
``````
``````using System;
using System.Collections;
public class SamplesArrayList  {

public static void Main()  {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintValues( myAL );

// Sorts the values of the ArrayList.
myAL.Sort();

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

public static void PrintValues( IEnumerable myList )  {
foreach ( Object obj in myList )
Console.WriteLine( "   {0}", obj );
Console.WriteLine();
}

}

/*
This code produces the following output.

The ArrayList initially contains the following values:
The
quick
brown
fox
jumps
over
the
lazy
dog

After sorting:
brown
dog
fox
jumps
lazy
over
quick
the
The
*/
``````
``````Imports System
Imports System.Collections

Public Class SamplesArrayList

Public Shared Sub Main()

' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()

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

' Sorts the values of the ArrayList.
myAL.Sort()

' Displays the values of the ArrayList.
Console.WriteLine("After sorting:")
PrintValues(myAL)

End Sub 'Main

Public Shared Sub PrintValues(myList As IEnumerable)
Dim obj As [Object]
For Each obj In  myList
Console.WriteLine("   {0}", obj)
Next obj
Console.WriteLine()
End Sub 'PrintValues

End Class 'SamplesArrayList

' This code produces the following output.
'
' The ArrayList initially contains the following values:
'    The
'    quick
'    brown
'    fox
'    jumps
'    over
'    the
'    lazy
'    dog
'
' After sorting:
'    brown
'    dog
'    fox
'    jumps
'    lazy
'    over
'    quick
'    the
'    The

``````

## Sort(IComparer)Sort(IComparer)Sort(IComparer)Sort(IComparer)

``````public:
virtual void Sort(System::Collections::IComparer ^ comparer);``````
``public virtual void Sort (System.Collections.IComparer comparer);``
``````abstract member Sort : System.Collections.IComparer -> unit
override this.Sort : System.Collections.IComparer -> unit``````
``Public Overridable Sub Sort (comparer As IComparer)``

#### 参数

comparer
IComparer IComparer IComparer IComparer

- 或 --or- 一个空引用（在 Visual Basic 中为 `Nothing`），将使用每个元素的 IComparable 实现。A null reference (`Nothing` in Visual Basic) to use the IComparable implementation of each element.

### 示例

``````using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
ref class myReverserClass: public IComparer
{
private:

// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
virtual int Compare( Object^ x, Object^ y ) sealed = IComparer::Compare
{
return ((gcnew CaseInsensitiveComparer)->Compare( y, x ));
}

};

int main()
{

// Creates and initializes a new ArrayList.
ArrayList^ myAL = gcnew ArrayList;

// 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();
Console::WriteLine( "After sorting with the default comparer:" );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer^ myComparer = gcnew myReverserClass;
myAL->Sort( myComparer );
Console::WriteLine( "After sorting with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );
}

void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( "\t[{0}]:\t{1}", i++, obj );
}

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 with the default comparer:
[0]:    brown
[1]:    dog
[2]:    fox
[3]:    jumps
[4]:    lazy
[5]:    over
[6]:    quick
[7]:    the
[8]:    The

After sorting with the reverse case-insensitive comparer:
[0]:    the
[1]:    The
[2]:    quick
[3]:    over
[4]:    lazy
[5]:    jumps
[6]:    fox
[7]:    dog
[8]:    brown
*/
``````
``````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();

// 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();
Console.WriteLine( "After sorting with the default comparer:" );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( myComparer );
Console.WriteLine( "After sorting with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );

}

public static void PrintIndexAndValues( IEnumerable myList )  {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( "\t[{0}]:\t{1}", i++, obj );
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 with the default comparer:
[0]:    brown
[1]:    dog
[2]:    fox
[3]:    jumps
[4]:    lazy
[5]:    over
[6]:    quick
[7]:    the
[8]:    The

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

``````
``````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()

' 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()
Console.WriteLine("After sorting with the default comparer:")
PrintIndexAndValues(myAL)

' Sorts the values of the ArrayList using the reverse case-insensitive comparer.
Dim myComparer = New myReverserClass()
myAL.Sort(myComparer)
Console.WriteLine("After sorting 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]:    jumps
'        [5]:    over
'        [6]:    the
'        [7]:    lazy
'        [8]:    dog
'
'After sorting with the default comparer:
'        [0]:    brown
'        [1]:    dog
'        [2]:    fox
'        [3]:    jumps
'        [4]:    lazy
'        [5]:    over
'        [6]:    quick
'        [7]:    the
'        [8]:    The
'
'After sorting with the reverse case-insensitive comparer:
'        [0]:    the
'        [1]:    The
'        [2]:    quick
'        [3]:    over
'        [4]:    lazy
'        [5]:    jumps
'        [6]:    fox
'        [7]:    dog
'        [8]:    brown

``````

## Sort(Int32, Int32, IComparer)Sort(Int32, Int32, IComparer)Sort(Int32, Int32, IComparer)Sort(Int32, Int32, IComparer)

``````public:
virtual void Sort(int index, int count, System::Collections::IComparer ^ comparer);``````
``public virtual void Sort (int index, int count, System.Collections.IComparer comparer);``
``````abstract member Sort : int * int * System.Collections.IComparer -> unit
override this.Sort : int * int * System.Collections.IComparer -> unit``````
``Public Overridable Sub Sort (index As Integer, count As Integer, comparer As IComparer)``

#### 参数

index
Int32 Int32 Int32 Int32

count
Int32 Int32 Int32 Int32

comparer
IComparer IComparer IComparer IComparer

- 或 --or- 一个空引用（在 Visual Basic 中为 `Nothing`），将使用每个元素的 IComparable 实现。A null reference (`Nothing` in Visual Basic) to use the IComparable implementation of each element.

#### 异常

`index` 小于零。`index` is less than zero.

- 或 --or- `count` 小于零。`count` is less than zero.

`index``count` 未在 ArrayList 中指定有效范围。`index` and `count` do not specify a valid range in the ArrayList.

### 示例

``````using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
ref class myReverserClass: public IComparer
{
private:

// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
virtual int Compare( Object^ x, Object^ y ) = IComparer::Compare
{
return ((gcnew CaseInsensitiveComparer)->Compare( y, x ));
}

};

int main()
{

// Creates and initializes a new ArrayList.
ArrayList^ myAL = gcnew ArrayList;

// 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, nullptr );
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 = gcnew myReverserClass;
myAL->Sort( 1, 3, myComparer );
Console::WriteLine( "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );
}

void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( "\t[{0}]:\t{1}", i++, obj );
}

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 from index 1 to index 3 with the default comparer:
[0]:    The
[1]:    BROWN
[2]:    FOX
[3]:    QUICK
[4]:    jumps
[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]:    jumps
[5]:    over
[6]:    the
[7]:    lazy
[8]:    dog
*/
``````
``````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();

// 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;
foreach ( Object obj in myList )
Console.WriteLine( "\t[{0}]:\t{1}", i++, obj );
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 from index 1 to index 3 with the default comparer:
[0]:    The
[1]:    BROWN
[2]:    FOX
[3]:    QUICK
[4]:    jumps
[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]:    jumps
[5]:    over
[6]:    the
[7]:    lazy
[8]:    dog
*/

``````
``````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()

' 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]:    jumps
'        [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]:    jumps
'        [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]:    jumps
'        [5]:    over
'        [6]:    the
'        [7]:    lazy
'        [8]:    dog

``````