英語で読む

次の方法で共有


ArrayList.Sort メソッド

定義

ArrayList またはその一部の要素を並べ替えます。

オーバーロード

Sort()

ArrayList 全体で要素を並べ替えます。

Sort(IComparer)

指定した比較子を使用して、ArrayList 全体内の要素を並べ替えます。

Sort(Int32, Int32, IComparer)

指定した比較子を使用して、ArrayList 内の要素の範囲内の要素を並べ替えます。

Sort()

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

ArrayList 全体で要素を並べ替えます。

C#
public virtual void Sort ();

例外

ArrayList は読み取り専用です。

次のコード例は、 の値を並べ替える方法を ArrayList示しています。

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

注釈

このメソッドでは、QuickSort アルゴリズムを使用する を使用 Array.Sortします。 QuickSort アルゴリズムは、比較並べ替え (不安定な並べ替えとも呼ばれます) です。つまり、"以下" の比較操作によって、最終的な並べ替えられた一覧で最初に 2 つの要素のうちどれが発生するかを決定します。 ただし、2 つの要素が等しい場合は、元の順序が保持されない可能性があります。 これに対し、安定した並べ替えでは、等しい要素の順序が保持されます。 安定した並べ替えを実行するには、このメソッドの他のオーバーロードで使用するカスタム IComparer インターフェイスを実装する必要があります。

平均して、このメソッドは O(n log n) 操作です。ここで n 、 は Countです。最悪の場合は操作です O(n^2)

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Sort(IComparer)

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

指定した比較子を使用して、ArrayList 全体内の要素を並べ替えます。

C#
public virtual void Sort (System.Collections.IComparer comparer);
C#
public virtual void Sort (System.Collections.IComparer? comparer);

パラメーター

comparer
IComparer

要素を比較する場合に使用する IComparer の実装。

- または -

各要素の IComparable 実装を使用するための null 参照 (Visual Basic の場合は Nothing)。

例外

ArrayList は読み取り専用です。

2 つの要素の比較中にエラーが発生しました。

nullcomparer として渡され、リスト内の要素が IComparable を実装していません。

次のコード例は、既定の比較子と、並べ替え順序を逆にするカスタム 比較子を使用して、 の ArrayList 値を並べ替える方法を示しています。

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

注釈

メソッドを Sort 使用して、 インターフェイスを実装するカスタム 比較子を使用してオブジェクトの一覧を IComparer 並べ替えます。 に をcomparer渡すnull場合、このメソッドは各要素の実装をIComparable使用します。 この場合は、リストに含まれるオブジェクトが インターフェイスを実装 IComparer しているか、例外が発生することを確認する必要があります。

さらに、 実装を IComparable 使用すると、リストは比較並べ替えを実行することを意味します (不安定な並べ替えとも呼ばれます)。つまり、2 つの要素が等しい場合は、順序が保持されない可能性があります。 これに対し、安定した並べ替えでは、等しい要素の順序が保持されます。 安定した並べ替えを実行するには、カスタム IComparer インターフェイスを実装する必要があります。

平均して、このメソッドは O(n log n) 操作です。ここで n 、 は Countです。最悪の場合は操作です O(n^2)

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Sort(Int32, Int32, IComparer)

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

指定した比較子を使用して、ArrayList 内の要素の範囲内の要素を並べ替えます。

C#
public virtual void Sort (int index, int count, System.Collections.IComparer comparer);
C#
public virtual void Sort (int index, int count, System.Collections.IComparer? comparer);

パラメーター

index
Int32

並べ替える範囲の開始位置を示す 0 から始まるインデックス。

count
Int32

並べ替える範囲の長さ。

comparer
IComparer

要素を比較する場合に使用する IComparer の実装。

- または -

各要素の IComparable 実装を使用するための null 参照 (Visual Basic の場合は Nothing)。

例外

index が 0 未満です。

または

count が 0 未満です。

index および countArrayList において有効な範囲を指定していません。

ArrayList は読み取り専用です。

2 つの要素の比較中にエラーが発生しました。

次のコード例では、既定の比較子と並べ替え順序を逆にするカスタム 比較子を使用して、 内の ArrayList 要素の範囲内の値を並べ替える方法を示します。

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

注釈

が にnull設定されている場合comparer、このメソッドは比較並べ替えを実行します (不安定な並べ替えとも呼ばれます)。つまり、2 つの要素が等しい場合は、順序が保持されない可能性があります。 これに対し、安定した並べ替えでは、等しい要素の順序が保持されます。 安定した並べ替えを実行するには、カスタム IComparer インターフェイスを実装する必要があります。

平均して、このメソッドは O(n log n) 操作です。ここで n 、 は countです。最悪の場合は操作です O(n^2)

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0