ArrayList.Sort メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
ArrayListまたはその一部の要素を並べ替えます。
オーバーロード
| 名前 | 説明 |
|---|---|
| Sort() |
ArrayList全体の要素を並べ替えます。 |
| Sort(IComparer) |
指定した比較子を使用して、 ArrayList 全体の要素を並べ替えます。 |
| Sort(Int32, Int32, IComparer) |
指定した比較子を使用して、 ArrayList 内の要素の範囲内の要素を並べ替えます。 |
Sort()
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
ArrayList全体の要素を並べ替えます。
public:
virtual void Sort();
public virtual void Sort();
abstract member Sort : unit -> unit
override this.Sort : unit -> unit
Public Overridable Sub Sort ()
例外
ArrayListは読み取り専用です。
例
次のコード例は、 ArrayListの値を並べ替える方法を示しています。
using System;
using System.Collections;
public class SamplesArrayList1
{
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:");
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()
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:")
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
' 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
注釈
このメソッドでは、QuickSort アルゴリズムを使用する Array.Sortを使用します。 QuickSort アルゴリズムは比較並べ替え (不安定な並べ替えとも呼ばれます) です。つまり、"以下" 比較操作によって、最終的な並べ替えられた一覧で最初に 2 つの要素のうちどれが発生するかを決定します。 ただし、2 つの要素が等しい場合は、元の順序が保持されない可能性があります。 これに対し、安定した並べ替えでは、等しい要素の順序が保持されます。 安定した並べ替えを実行するには、このメソッドの他のオーバーロードで使用するカスタム IComparer インターフェイスを実装する必要があります。
平均して、このメソッドは O(n log n) 操作であり、 n は Count。最悪の場合は O(n^2) 操作です。
こちらもご覧ください
適用対象
Sort(IComparer)
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
指定した比較子を使用して、 ArrayList 全体の要素を並べ替えます。
public:
virtual void Sort(System::Collections::IComparer ^ comparer);
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 実装。
-又は-
各要素の Nothing 実装を使用するための null 参照 (Visual Basic の IComparable)。
例外
ArrayListは読み取り専用です。
2 つの要素の比較中にエラーが発生しました。
null は comparerに渡され、リスト内の要素は IComparableを実装しません。
例
次のコード例は、既定の比較子と並べ替え順序を逆にするカスタム比較子を使用して、 ArrayList の値を並べ替える方法を示しています。
using System;
using System.Collections;
public class SamplesArrayList2
{
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("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 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
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 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
'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 メソッドを使用して、IComparer インターフェイスを実装するカスタム比較子を使用してオブジェクトの一覧を並べ替えます。
nullのcomparerを渡す場合、このメソッドは各要素のIComparable実装を使用します。 この場合、リストに含まれるオブジェクトが IComparer インターフェイスを実装しているか、例外が発生することを確認する必要があります。
さらに、 IComparable 実装を使用すると、リストは比較並べ替えを実行することを意味します (不安定な並べ替えとも呼ばれます)。つまり、2 つの要素が等しい場合、順序が保持されない可能性があります。 これに対し、安定した並べ替えでは、等しい要素の順序が保持されます。 安定した並べ替えを実行するには、カスタム IComparer インターフェイスを実装する必要があります。
平均して、このメソッドは O(n log n) 操作であり、 n は Count。最悪の場合は O(n^2) 操作です。
こちらもご覧ください
適用対象
Sort(Int32, Int32, IComparer)
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
- ソース:
- ArrayList.cs
指定した比較子を使用して、 ArrayList 内の要素の範囲内の要素を並べ替えます。
public:
virtual void Sort(int index, int count, System::Collections::IComparer ^ comparer);
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
並べ替える範囲の 0 から始まる開始インデックス。
- count
- Int32
並べ替える範囲の長さ。
- comparer
- IComparer
要素を比較するときに使用する IComparer 実装。
-又は-
各要素の Nothing 実装を使用するための null 参照 (Visual Basic の IComparable)。
例外
index と count は、 ArrayListで有効な範囲を指定しません。
ArrayListは読み取り専用です。
2 つの要素の比較中にエラーが発生しました。
例
次のコード例は、既定の比較子と並べ替え順序を逆にするカスタム比較子を使用して、 ArrayList 内の要素の範囲内の値を並べ替える方法を示しています。
using System;
using System.Collections;
public class SamplesArrayList3
{
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("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 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
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 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
'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 つの要素が等しい場合、順序が保持されない可能性があります。 これに対し、安定した並べ替えでは、等しい要素の順序が保持されます。 安定した並べ替えを実行するには、カスタム IComparer インターフェイスを実装する必要があります。
平均して、このメソッドは O(n log n) 操作であり、 n は count。最悪の場合は O(n^2) 操作です。