ArrayList.Sort Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Sortuje elementy w ArrayList części lub .
Przeciążenia
| Nazwa | Opis |
|---|---|
| Sort() |
Sortuje elementy w całym ArrayList. |
| Sort(IComparer) |
Sortuje elementy w całej ArrayList przy użyciu określonego modułu porównującego. |
| Sort(Int32, Int32, IComparer) |
Sortuje elementy w zakresie elementów w ArrayList przy użyciu określonego porównania. |
Sort()
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
Sortuje elementy w całym ArrayList.
public:
virtual void Sort();
public virtual void Sort();
abstract member Sort : unit -> unit
override this.Sort : unit -> unit
Public Overridable Sub Sort ()
Wyjątki
Element ArrayList jest tylko do odczytu.
Przykłady
Poniższy przykład kodu przedstawia sposób sortowania wartości w obiekcie 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
Uwagi
Ta metoda używa Array.Sortalgorytmu , który używa algorytmu QuickSort. Algorytm QuickSort jest sortowaniem porównania (nazywanym również niestabilnym sortowaniem), co oznacza, że operacja porównania "mniejsza niż lub równa" określa, które z dwóch elementów powinny wystąpić najpierw na końcowej liście posortowanej. Jeśli jednak dwa elementy są równe, ich oryginalna kolejność może nie zostać zachowana. Natomiast stabilny sortowanie zachowuje kolejność elementów, które są równe. Aby przeprowadzić stabilne sortowanie, należy zaimplementować interfejs niestandardowy IComparer do użycia z innymi przeciążeniami tej metody.
Ta metoda jest operacją O(n log n) , gdzie n jest Count; w najgorszym przypadku jest to O(n^2) operacja.
Zobacz też
Dotyczy
Sort(IComparer)
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
Sortuje elementy w całej ArrayList przy użyciu określonego modułu porównującego.
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)
Parametry
- comparer
- IComparer
Implementacja IComparer do użycia podczas porównywania elementów.
— lub —
Odwołanie o wartości null (Nothing w Visual Basic) w celu użycia implementacji IComparable poszczególnych elementów.
Wyjątki
Element ArrayList jest tylko do odczytu.
Wystąpił błąd podczas porównywania dwóch elementów.
null parametr jest przekazywany dla comparerelementu , a elementy na liście nie implementują IComparableelementu .
Przykłady
W poniższym przykładzie kodu pokazano, jak sortować wartości przy ArrayList użyciu domyślnego modułu porównującego i niestandardowego porównania, który odwraca kolejność sortowania.
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
Uwagi
Sort Użyj metody , aby posortować listę obiektów z niestandardowym modułem porównującym, który implementuje IComparer interfejs. W przypadku przekazania null metody comparerta metoda używa IComparable implementacji każdego elementu. W takim przypadku należy upewnić się, że obiekty znajdujące się na liście implementują IComparer interfejs lub wystąpi wyjątek.
Ponadto użycie IComparable implementacji oznacza, że lista wykonuje sortowanie porównania (nazywane również niestabilnym sortowaniem), czyli, jeśli dwa elementy są równe, ich kolejność może nie zostać zachowana. Natomiast stabilny sortowanie zachowuje kolejność elementów, które są równe. Aby przeprowadzić stabilne sortowanie, należy zaimplementować interfejs niestandardowy IComparer .
Ta metoda jest operacją O(n log n) , gdzie n jest Count; w najgorszym przypadku jest to O(n^2) operacja.
Zobacz też
Dotyczy
Sort(Int32, Int32, IComparer)
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
- Źródło:
- ArrayList.cs
Sortuje elementy w zakresie elementów w ArrayList przy użyciu określonego porównania.
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)
Parametry
- index
- Int32
Zerowy indeks początkowy zakresu do sortowania.
- count
- Int32
Długość zakresu do sortowania.
- comparer
- IComparer
Implementacja IComparer do użycia podczas porównywania elementów.
— lub —
Odwołanie o wartości null (Nothing w Visual Basic) w celu użycia implementacji IComparable poszczególnych elementów.
Wyjątki
Parametr index ma wartość niższą niż zero.
— lub —
Parametr count ma wartość niższą niż zero.
index i count nie należy określać prawidłowego zakresu w obiekcie ArrayList.
Element ArrayList jest tylko do odczytu.
Wystąpił błąd podczas porównywania dwóch elementów.
Przykłady
W poniższym przykładzie kodu pokazano, jak sortować wartości w zakresie elementów przy ArrayList użyciu domyślnego modułu porównania i niestandardowego porównania, który odwraca kolejność sortowania.
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
Uwagi
Jeśli comparer jest ustawiona nullwartość , ta metoda wykonuje sortowanie porównania (nazywane również niestabilnym sortowaniem), czyli jeśli dwa elementy są równe, ich kolejność może nie zostać zachowana. Natomiast stabilny sortowanie zachowuje kolejność elementów, które są równe. Aby przeprowadzić stabilne sortowanie, należy zaimplementować interfejs niestandardowy IComparer .
Ta metoda jest operacją O(n log n) , gdzie n jest count; w najgorszym przypadku jest to O(n^2) operacja.