ArrayList.Sort Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Sortiert die Elemente in dem ArrayList oder einem Teil davon.
Überlädt
| Name | Beschreibung |
|---|---|
| Sort() |
Sortiert die Elemente im gesamten ArrayList. |
| Sort(IComparer) |
Sortiert die Elemente im gesamten ArrayList Element mithilfe des angegebenen Vergleichs. |
| Sort(Int32, Int32, IComparer) |
Sortiert die Elemente in einem Bereich von Elementen mithilfe ArrayList des angegebenen Vergleichs. |
Sort()
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
Sortiert die Elemente im gesamten ArrayList.
public:
virtual void Sort();
public virtual void Sort();
abstract member Sort : unit -> unit
override this.Sort : unit -> unit
Public Overridable Sub Sort ()
Ausnahmen
Dies ArrayList ist schreibgeschützt.
Beispiele
Das folgende Codebeispiel zeigt, wie die Werte in einer 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
Hinweise
Diese Methode verwendet Array.Sortden QuickSort-Algorithmus. Der QuickSort-Algorithmus ist eine Vergleichssortierung (auch als instabile Sortierung bezeichnet), was bedeutet, dass ein Vergleichsvorgang "kleiner als oder gleich" bestimmt, welche von zwei Elementen zuerst in der endgültigen sortierten Liste auftreten sollen. Wenn zwei Elemente jedoch gleich sind, wird ihre ursprüngliche Reihenfolge möglicherweise nicht beibehalten. Im Gegensatz dazu behält eine stabile Sortierung die Reihenfolge von Elementen bei, die gleich sind. Um eine stabile Sortierung durchzuführen, müssen Sie eine benutzerdefinierte IComparer Schnittstelle implementieren, die mit den anderen Überladungen dieser Methode verwendet werden soll.
Im Durchschnitt handelt es sich bei dieser Methode um einen O(n log n) Vorgang, bei n dem es sich im schlimmsten Fall um einen Count Vorgang handeltO(n^2).
Weitere Informationen
Gilt für:
Sort(IComparer)
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
Sortiert die Elemente im gesamten ArrayList Element mithilfe des angegebenen Vergleichs.
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)
Parameter
- comparer
- IComparer
Die IComparer Implementierung, die beim Vergleichen von Elementen verwendet werden soll.
-oder-
Ein Nullverweis (Nothing in Visual Basic), um die implementierung der einzelnen Elemente IComparable zu verwenden.
Ausnahmen
Dies ArrayList ist schreibgeschützt.
Fehler beim Vergleichen von zwei Elementen.
null wird für comparer, und die Elemente in der Liste werden nicht implementiert IComparable.
Beispiele
Das folgende Codebeispiel zeigt, wie die Werte in einem ArrayList Standardabgleich und einem benutzerdefinierten Vergleichselement sortiert werden, mit dem die Sortierreihenfolge umgekehrt wird.
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
Hinweise
Verwenden Sie die Sort Methode, um eine Liste von Objekten mit einem benutzerdefinierten Vergleich zu sortieren, der die IComparer Schnittstelle implementiert. Wenn Sie dies übergeben nullcomparer, verwendet diese Methode die IComparable Implementierung der einzelnen Elemente. In diesem Fall müssen Sie sicherstellen, dass die in der Liste enthaltenen Objekte die IComparer Schnittstelle implementieren oder eine Ausnahme auftritt.
Darüber hinaus bedeutet die Verwendung der IComparable Implementierung, dass die Liste eine Vergleichssortierung durchführt (auch als instabile Sortierung bezeichnet), d. h. wenn zwei Elemente gleich sind, wird ihre Reihenfolge möglicherweise nicht beibehalten. Im Gegensatz dazu behält eine stabile Sortierung die Reihenfolge von Elementen bei, die gleich sind. Um eine stabile Sortierung durchzuführen, müssen Sie eine benutzerdefinierte IComparer Schnittstelle implementieren.
Im Durchschnitt handelt es sich bei dieser Methode um einen O(n log n) Vorgang, bei n dem es sich im schlimmsten Fall um einen Count Vorgang handeltO(n^2).
Weitere Informationen
Gilt für:
Sort(Int32, Int32, IComparer)
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
- Quelle:
- ArrayList.cs
Sortiert die Elemente in einem Bereich von Elementen mithilfe ArrayList des angegebenen Vergleichs.
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)
Parameter
- index
- Int32
Der nullbasierte Anfangsindex des zu sortierenden Bereichs.
- count
- Int32
Die Länge des zu sortierenden Bereichs.
- comparer
- IComparer
Die IComparer Implementierung, die beim Vergleichen von Elementen verwendet werden soll.
-oder-
Ein Nullverweis (Nothing in Visual Basic), um die implementierung der einzelnen Elemente IComparable zu verwenden.
Ausnahmen
index und count geben Sie keinen gültigen Bereich in der ArrayList.
Dies ArrayList ist schreibgeschützt.
Fehler beim Vergleichen von zwei Elementen.
Beispiele
Das folgende Codebeispiel zeigt, wie die Werte in einem Elementbereich mithilfe ArrayList des Standardabgleichs und eines benutzerdefinierten Vergleichs sortiert werden, der die Sortierreihenfolge umkehrt.
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
Hinweise
Wenn diese Methode auf comparer festgelegt ist, führt diese Methode eine Vergleichssortierung aus (auch als instabile Sortierung bezeichnet). Wenn null zwei Elemente gleich sind, wird die Reihenfolge möglicherweise nicht beibehalten. Im Gegensatz dazu behält eine stabile Sortierung die Reihenfolge von Elementen bei, die gleich sind. Um eine stabile Sortierung durchzuführen, müssen Sie eine benutzerdefinierte IComparer Schnittstelle implementieren.
Im Durchschnitt handelt es sich bei dieser Methode um einen O(n log n) Vorgang, bei n dem es sich im schlimmsten Fall um einen count Vorgang handeltO(n^2).