Array.Sort方法用于对一维数组中的元素进行排序。此方法的重载列表中有17种方法,如下所示:
- Sort
(T [])方法 - Sort
(T [],IComparer )方法 - Sort
(T [],Int32,Int32)方法 - Sort
(T [],比较 )方法 - Sort(Array,Int32,Int32,IComparer)方法
- Sort(Array,Array,Int32,Int32,IComparer)方法
- Sort(Array,Int32,Int32)方法
- Sort(Array,Array,Int32,Int32)方法
- Sort(Array,IComparer)方法
- Sort(Array,Array,IComparer)方法
- Sort(Array,Array)方法
- Sort(Array)方法
- Sort
(T [],Int32,Int32,IComparer )方法 - Sort
(TKey [],TValue [])方法 - Sort
(TKey [],TValue [],IComparer )方法 - Sort
(TKey [],TValue [],Int32,Int32)方法 - Sort
(TKey [],TValue [],Int32,Int32,IComparer )方法
在这里,我们将讨论前4种方法。
Sort (T [])方法
此方法使用Array每个元素的IComparable
Syntax: public static void Sort
Parameter:
array: It is the one dimensional, zero-based Array which is to be sorted.
例外情况:
- ArgumentNullException:如果数组为null。
- InvalidOperationException:如果数组中的一个或多个元素未实现IComparable
通用接口。
例子:
// C# Program to illustrate the use
// of the Array.Sort(T[]) Method
using System;
using System.Collections.Generic;
class GFG {
// Main Method
public static void Main()
{
// array elements
string[] arr = new string[5] { "A",
"D", "X", "G", "M" };
foreach(string g in arr)
{
Console.WriteLine(g);
// display original array
}
Console.WriteLine("\nAfter Sort:");
Array.Sort(arr);
foreach(string g in arr)
{
Console.WriteLine(g);
// display sorted array
}
Console.WriteLine("\nB sorts between :");
// binary Search for "B"
int index = Array.BinarySearch(arr, "B");
// call "sortT" function
// which is the Sort(T[]) function
sortT(arr, index);
Console.WriteLine("\nF sorts between :");
index = Array.BinarySearch(arr, "F");
sortT(arr, index);
}
public static void sortT(T[] arr, int index)
{
// If the index is negative,
// it represents the bitwise
// complement of the next larger
// element in the array.
if (index < 0) {
index = ~index;
if (index == 0)
Console.Write("beginning of array");
else
Console.Write("{0} and ", arr[index - 1]);
if (index == arr.Length)
Console.WriteLine("end of array.");
else
Console.WriteLine("{0}", arr[index]);
}
}
}
A
D
X
G
M
After Sort:
A
D
G
M
X
B sorts between :
A and D
F sorts between :
D and G
Sort (T [],IComparer )方法
此方法使用指定的IComparer
Syntax: public static void Sort
Parameters:
- T : It is the type of the elements of the array.
- array : It is the one-dimensional Array which is to be sorted.
- comparer : It is the IComparer
generic interface implementation to use when comparing elements or null to use the IComparable generic interface implementation of each element.
例外情况:
- ArgumentNullException:如果数组为null。
- InvalidOperationException:如果比较器为null,并且没有IComparable
通用接口的实现。 - ArgumentException:
如果比较器的实现在排序过程中引起了错误。
例子:
// C# program to demonstrate the use of the
// Array.Sort(T[], IComparer) method
using System;
using System.Collections.Generic;
public class GeeK : IComparer {
public int Compare(string x, string y)
{
// Compare x and y in reverse order.
return x.CompareTo(y);
}
}
class GFG {
// Main Method
public static void Main()
{
// array elements
string[] arr = new string[5] {"A",
"D", "X", "G", "M" };
foreach(string g in arr)
{
// display original array
Console.WriteLine(g);
}
Console.WriteLine("\nAfter Sort: ");
GeeK gg = new GeeK();
// Sort(T[], IComparer) method
Array.Sort(arr, gg);
foreach(string g in arr)
{
// display sorted array
Console.WriteLine(g);
}
Console.WriteLine("\nD Sorts between :");
// binary Search for "D"
int index = Array.BinarySearch(arr, "D");
// call "sortT" function
sortT(arr, index);
Console.WriteLine("\nF Sorts between :");
index = Array.BinarySearch(arr, "F");
sortT(arr, index);
}
public static void sortT(T[]arr, int index)
{
if (index < 0)
{
// If the index is negative,
// it represents the bitwise
// complement of the next
// larger element in the array.
index = ~index;
Console.Write("Not found. Sorts between: ");
if (index == 0)
Console.Write("Beginning of array and ");
else
Console.Write("{0} and ", arr[index-1]);
if (index == arr.Length)
Console.WriteLine("end of array.");
else
Console.WriteLine("{0}.", arr[index]);
}
else
{
Console.WriteLine("Found at index {0}.", index);
}
}
}
A
D
X
G
M
After Sort:
A
D
G
M
X
D Sorts between :
Found at index 1.
F Sorts between :
Not found. Sorts between: D and G.
Array.Sort (T [],Int32,Int32)方法
此方法使用Array中每个元素的IComparable
Syntax: public static void Sort
Parameters:
- array: It is the one-dimensional, zero-based Array to sort.
- index: It is the starting index of the range to sort.
- length: It is the number of elements in the range to sort.
例外情况:
- ArgumentNullException:如果数组为null。
- ArgumentOutOfRangeException:如果索引小于数组的下限或长度小于零。
- ArgumentException:如果索引和长度未在数组中指定有效范围。
- InvalidOperationException:如果数组中的一个或多个元素未实现IComparable
通用接口。
例子:
// C# program to demonstrate the use of
// Array.Sort(T[], Int32, Int32) method
using System;
using System.Collections.Generic;
public class Geek : IComparer {
public int Compare(string x, string y)
{
// Compare y and x in reverse order.
return y.CompareTo(x);
}
}
public class Example {
// Main Method
public static void Main()
{
// Array elements
string[] arr = {"AB", "CD",
"GH", "EF", "MN", "IJ"};
Console.WriteLine("Original Array :");
Display(arr);
Console.WriteLine("\nSort the array between "+
"index 1 to 4");
// Array.Sort(T[], Int32, Int32) method
// sort will happen in between
// index 1 to 4
Array.Sort(arr, 1, 4);
Display(arr);
Console.WriteLine("\nSort the array reversely"+
" in between index 1 to 4");
// sort will happen in between
// index 1 to 4 reversely
Array.Sort(arr, 1, 4, new Geek());
Display(arr);
}
public static void Display(string[] arr)
{
foreach(string g in arr)
{
Console.WriteLine(g);
}
}
}
Original Array :
AB
CD
GH
EF
MN
IJ
Sort the array between index 1 to 4
AB
CD
EF
GH
MN
IJ
Sort the array reversely in between index 1 to 4
AB
MN
GH
EF
CD
IJ
Array.Sort (T [],比较)方法
此方法使用指定的Comparison
Syntax: public static void Sort
Parameters:
- array: It is the one-dimensional zero-based Array which is to be sorted.
- comparison: It is the comparison
to used when comparing elements.
例外情况:
- ArgumentNullException :如果数组为null或比较为null。
- ArgumentException :如果比较的实现在排序期间导致错误。
例子:
// C# program to demonstrate the use of the
// Array.Sort(T[ ], Comparison) Method
using System;
using System.Collections.Generic;
class GFG {
private static int CompareComp(string x, string y)
{
if (y == null && x == null) {
// If x and y is null
// then x and y are same
return 0;
}
else {
// If x is null but y is not
// null then y is greater.
return -1;
}
}
// Main method
public static void Main()
{
string[] arr = {"Java", "C++", "Scala",
"C", "Ruby", "Python"};
Console.WriteLine("Original Array: ");
// display original array
Display(arr);
Console.WriteLine("\nSort with Comparison: ");
// Array.Sort(T[], Comparison)
// Method
Array.Sort(arr, CompareComp);
// display sorted array
Display(arr);
}
// Display function
public static void Display(string[] arr)
{
foreach(string g in arr)
{
Console.WriteLine(g);
}
}
}
Original Array:
Java
C++
Scala
C
Ruby
Python
Sort with Comparison:
Python
Ruby
C
Scala
C++
Java
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array.sort?view=netframework-4.7.2