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

ArrayList 全体で要素を並べ替えます。Sorts the elements in the entire ArrayList.

``````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.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

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

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

``````

### 注釈

このメソッドはArray.Sort、クイックソートアルゴリズムを使用するを使用します。This method uses Array.Sort, which uses the QuickSort algorithm. クイックソートアルゴリズムは、比較の並べ替え ("不安定な並べ替え" とも呼ばれます) です。これは、"以下" の比較操作によって、最後の並べ替えられたリストの中で2つの要素のどちらが最初に出現する必要があるかを決定することを意味します。The QuickSort 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. ただし、2つの要素が等しい場合、元の順序は保持されない可能性があります。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. 安定した並べ替えを実行するには、このIComparerメソッドの他のオーバーロードと共に使用するカスタムインターフェイスを実装する必要があります。To perform a stable sort, you must implement a custom IComparer interface to use with the other overloads of this method.

こちらもご覧ください

## 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- 各要素の IComparable 実装を使用するための null 参照 (Visual Basic の場合は `Nothing`)。A null reference (`Nothing` in Visual Basic) to use the IComparable implementation of each element.

#### 例外

2 つの要素の比較中にエラーが発生しました。An error occurred while comparing two elements.

`null``comparer` として渡され、リスト内の要素が IComparable を実装していません。`null` is passed for `comparer`, and the elements in the list do not implement IComparable.

### 例

``````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.Collections

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

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

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

``````

### 注釈

インターフェイスIComparerSort実装するカスタム比較子を使用してオブジェクトの一覧を並べ替えるには、メソッドを使用します。Use the Sort method to sort a list of objects with a custom comparer that implements the IComparer interface. にを`null` `comparer`渡す場合、このメソッドは各IComparable要素の実装を使用します。If you pass `null` for `comparer`, this method uses the IComparable implementation of each element. この場合は、リストに含まれているオブジェクトがインターフェイスをIComparer実装していることを確認する必要があります。そうしないと、例外が発生します。In this case, you must make sure that the objects contained in the list implement the IComparer interface or an exception will occur.

また、 IComparable実装を使用して、リストが比較並べ替え (不安定な並べ替えとも呼ばれます) を実行することを意味します。つまり、2つの要素が等しい場合、その順序は保持されない可能性があります。In addition, using the IComparable implementation means the list 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. 安定した並べ替えを実行するには、カスタムIComparerインターフェイスを実装する必要があります。To perform a stable sort, you must implement a custom IComparer interface.

こちらもご覧ください

## 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- 各要素の IComparable 実装を使用するための null 参照 (Visual Basic の場合は `Nothing`)。A null reference (`Nothing` in Visual Basic) to use the IComparable implementation of each element.

#### 例外

`index` が 0 未満です。`index` is less than zero.

- または --or- `count` が 0 未満です。`count` is less than zero.

`index` および `count`ArrayList において有効な範囲を指定していません。`index` and `count` do not specify a valid range in the ArrayList.

2 つの要素の比較中にエラーが発生しました。An error occurred while comparing two elements.

### 例

``````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.Collections

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

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

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

``````

### 注釈

`comparer``null`設定されている場合、このメソッドは比較並べ替え (不安定な並べ替えとも呼ばれます) を実行します。つまり、2つの要素が等しい場合、その順序は保持されない可能性があります。If `comparer` is set to `null`, 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. 安定した並べ替えを実行するには、カスタムIComparerインターフェイスを実装する必要があります。To perform a stable sort, you must implement a custom IComparer interface.

こちらもご覧ください