ArrayList.Sort 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对元素中的 ArrayList 元素或部分进行排序。
重载
| 名称 | 说明 |
|---|---|
| Sort() |
对整个元素进行 ArrayList排序。 |
| Sort(IComparer) |
使用指定的比较器对整个 ArrayList 元素进行排序。 |
| Sort(Int32, Int32, IComparer) |
使用指定的比较器对一系列元素中的 ArrayList 元素进行排序。 |
Sort()
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
对整个元素进行 ArrayList排序。
public:
virtual void Sort();
public virtual void Sort();
abstract member Sort : unit -> unit
override this.Sort : unit -> unit
Public Overridable Sub Sort ()
例外
示例
下面的代码示例演示如何对值进行 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
注解
此方法使用 Array.SortQuickSort 算法。 QuickSort 算法是一种比较排序(也称为不稳定排序),这意味着“小于或等于”比较操作确定最终排序列表中应首先出现两个元素中的哪一个。 但是,如果两个元素相等,则其原始顺序可能不会保留。 相比之下,稳定的排序会保留相等元素的顺序。 若要执行稳定的排序,必须实现自定义 IComparer 接口,以便与此方法的其他重载一起使用。
平均而言,此方法是一个 O(n log n) 操作,其中 n 为 Count;在最坏的情况下,它是一个 O(n^2) 操作。
另请参阅
适用于
Sort(IComparer)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
使用指定的比较器对整个 ArrayList 元素进行排序。
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)
参数
- comparer
- IComparer
IComparer比较元素时要使用的实现。
-或-
null 引用(Visual Basic 中的 Nothing)使用每个元素的 IComparable 实现。
例外
比较两个元素时出错。
null 是传递的 comparer,列表中元素不实现 IComparable。
示例
下面的代码示例演示如何使用默认比较器和一个反转排序顺序的自定义比较器对值 ArrayList 进行排序。
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
注解
Sort使用该方法对实现IComparer接口的自定义比较器的对象列表进行排序。 如果传递 null , comparer此方法将使用 IComparable 每个元素的实现。 在这种情况下,必须确保列表中所包含的对象实现 IComparer 接口或发生异常。
此外,使用 IComparable 实现意味着列表执行比较排序(也称为不稳定排序);也就是说,如果两个元素相等,则其顺序可能不会保留。 相比之下,稳定的排序会保留相等元素的顺序。 若要执行稳定的排序,必须实现自定义 IComparer 接口。
平均而言,此方法是一个 O(n log n) 操作,其中 n 为 Count;在最坏的情况下,它是一个 O(n^2) 操作。
另请参阅
适用于
Sort(Int32, Int32, IComparer)
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
- Source:
- ArrayList.cs
使用指定的比较器对一系列元素中的 ArrayList 元素进行排序。
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)
参数
- index
- Int32
要排序的范围从零开始的起始索引。
- count
- Int32
要排序的范围的长度。
- comparer
- IComparer
IComparer比较元素时要使用的实现。
-或-
null 引用(Visual Basic 中的 Nothing)使用每个元素的 IComparable 实现。
例外
index 且 count 未在 . 中 ArrayList指定有效范围。
比较两个元素时出错。
示例
下面的代码示例演示如何使用默认比较器和一个反转排序顺序的自定义比较器对元素范围内的 ArrayList 值进行排序。
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
注解
如果 comparer 设置为 null,则此方法执行比较排序(也称为不稳定排序);也就是说,如果两个元素相等,则它们的顺序可能不会保留。 相比之下,稳定的排序会保留相等元素的顺序。 若要执行稳定的排序,必须实现自定义 IComparer 接口。
平均而言,此方法是一个 O(n log n) 操作,其中 n 为 count;在最坏的情况下,它是一个 O(n^2) 操作。