ArrayList.Sort 메서드

정의

ArrayList 또는 그 일부에 포함된 요소를 정렬합니다.Sorts the elements in the ArrayList or a portion of it.

오버로드

Sort()

전체 ArrayList의 요소를 정렬합니다.Sorts the elements in the entire ArrayList.

Sort(IComparer)

지정된 비교자를 사용하여 전체 ArrayList에 있는 요소를 정렬합니다.Sorts the elements in the entire ArrayList using the specified comparer.

Sort(Int32, Int32, IComparer)

지정된 비교자를 사용하여 ArrayList의 요소 범위에 있는 요소를 정렬합니다.Sorts the elements in a range of elements in ArrayList using the specified comparer.

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

예외

ArrayList이 읽기 전용인 경우The ArrayList is read-only.

예제

다음 코드 예제에서는 값을 정렬 하는 방법을 보여 줍니다는 ArrayList합니다.The following code example shows how to sort the values in an ArrayList.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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 );
}

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

설명

이 메서드는 Array.Sort, 빠른 정렬 알고리즘을 사용 하는 합니다.This method uses Array.Sort, which uses the QuickSort algorithm. 빠른 정렬 알고리즘은 비교 (불안정 한 정렬을 라고도 함)는 "보다 작거나 같음"을 의미 하는 정렬 비교 연산을 결정 어떤 두 요소 값은 최종 정렬 된 목록에서 먼저 수행 되어야 합니다.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. 그러나 두 요소가 같은 경우 원래 순서 유지 되지 않을 수 있습니다.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.

평균적으로이 메서드는 0 @no__t 연산이 며 nCount입니다. 최악의 경우에는 O(n^2) 연산이 수행 됩니다.On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n^2) operation.

추가 정보

Sort(IComparer)

지정된 비교자를 사용하여 전체 ArrayList에 있는 요소를 정렬합니다.Sorts the elements in the entire ArrayList using the specified 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 구현입니다.The IComparer implementation to use when comparing elements.

또는-or- 각 요소의 IComparable 구현을 사용할 null 참조(Visual Basic에서는 Nothing)입니다.A null reference (Nothing in Visual Basic) to use the IComparable implementation of each element.

예외

ArrayList이 읽기 전용인 경우The ArrayList is read-only.

두 요소를 비교하는 동안 오류가 발생했습니다.An error occurred while comparing two elements.

comparer에 대해 null이 전달되었으며, 목록의 요소가 IComparable을 구현하지 않습니다.null is passed for comparer, and the elements in the list do not implement IComparable.

예제

다음 코드 예제에서는 값을 정렬 하는 방법을 보여 줍니다는 ArrayList 역순으로 정렬 하는 사용자 지정 비교자와 기본 비교자를 사용 하 여 합니다.The following code example shows how to sort the values in an ArrayList using the default comparer and a custom comparer that reverses the sort order.

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;
   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 = 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();
      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 인터페이스.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 구현 (불안정 한 정렬을 라고도 함)을 비교 정렬 수행 하는 목록 의미, 즉, 두 요소가 같은 경우 순서 수 유지 되지 합니다.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.

평균적으로이 메서드는 0 @no__t 연산이 며 nCount입니다. 최악의 경우에는 O(n^2) 연산이 수행 됩니다.On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n^2) operation.

추가 정보

Sort(Int32, Int32, IComparer)

지정된 비교자를 사용하여 ArrayList의 요소 범위에 있는 요소를 정렬합니다.Sorts the elements in a range of elements in ArrayList using the specified 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부터 시작하는 인덱스입니다.The zero-based starting index of the range to sort.

count
Int32

정렬할 범위의 길이입니다.The length of the range to sort.

comparer
IComparer

요소를 비교할 때 사용하는 IComparer 구현입니다.The IComparer implementation to use when comparing elements.

또는-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.

indexcountArrayList의 올바른 범위를 지정하지 않습니다.index and count do not specify a valid range in the ArrayList.

ArrayList이 읽기 전용인 경우The ArrayList is read-only.

두 요소를 비교하는 동안 오류가 발생했습니다.An error occurred while comparing two elements.

예제

다음 코드 예제에서 요소의 범위에서 값을 정렬 하는 방법을 보여 줍니다는 ArrayList 역순으로 정렬 하는 사용자 지정 비교자와 기본 비교자를 사용 하 여 합니다.The following code example shows how to sort the values in a range of elements in an ArrayList using the default comparer and a custom comparer that reverses the sort order.

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;
   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, 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();
      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,이 메서드가 수행 (불안정 한 정렬을 라고도 함)을 비교 정렬, 즉, 두 요소가 같은 경우 순서 수 유지 되지 합니다.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.

평균적으로이 메서드는 0 @no__t 연산이 며 ncount입니다. 최악의 경우에는 O(n^2) 연산이 수행 됩니다.On average, this method is an O(n log n) operation, where n is count; in the worst case it is an O(n^2) operation.

추가 정보

적용 대상