次の方法で共有


ArrayList.Sort メソッド (Int32, Int32, IComparer)

指定した比較演算子を使用して、 ArrayList のセクション内の要素を並べ替えます。

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

パラメータ

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

  • count
    並べ替える範囲の長さ。

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

    または

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

例外

例外の種類 条件
ArgumentOutOfRangeException index が 0 未満です。

または

count が 0 未満です。

ArgumentException index および count が ArrayList 内の有効な範囲を指定していません。
NotSupportedException ArrayList が読み取り専用です。

解説

このメソッドは、QuickSort アルゴリズムを使用する Array.Sort を使用します。このアルゴリズムは O(n ^2) 操作です。 n は、並べ替える要素の数を示し、平均操作回数は θ(n log n) です。

この実装では不安定な並べ替えを実行します。つまり、2 つの要素が等しかった場合、これらの順序は保持されない可能性があります。一方、安定した並べ替えでは、等しい要素の順序が保持されます。

使用例

[Visual Basic, C#, C++] 既定の比較演算子と並べ替え順序を反転するカスタム比較演算子を使用して、 ArrayList のセクション内の値を並べ替える方法を次の例に示します。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | ArrayList.Sort オーバーロードの一覧 | カルチャを認識しないコレクションの操作の実行