Array.Sort方法用于对一维数组中的元素进行排序。此方法的重载列表中有17种方法。在这里,我们将讨论以下方法:
- Sort(Array,IComparer)方法
- Sort(Array,Array,IComparer)方法
- Sort(Array,Array)方法
Sort(Array,IComparer)方法
此方法使用指定的IComparer对一维数组中的元素进行排序。
Syntax: public static void Sort (Array arr, IComparer comparer);
Parameters:
arr: It is the one-dimensional array to sort.
comparer: It is the implementation to use when comparing elements.
例外情况:
- ArgumentNullException:如果数组arr为null。
- RankException:如果数组arr是多维的。
- InvalidOperationException:如果比较器为null。
- ArgumentException:如果比较器的实现在排序期间导致错误。
范例1:
// C# program to demonstrate the
// Array.Sort(Array, IComparer) method
using System;
using System.Collections;
class compare : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(x, y);
}
}
class GFG {
// Main Method
public static void Main()
{
// Initializing array.
String[] arr = {"A", "D", "B",
"E", "C", "F", "G"};
// Instantiate the IComparer object
IComparer cmp = new compare();
// Display the original values of the array
Console.WriteLine("The Original array:");
display(arr);
// Sort the entire array by using
// the IComparer object
// "cmp" is the IComparer object
Array.Sort(arr, cmp);
Console.WriteLine("\nAfter sorting the array"+
" using the IComparer:");
display(arr);
}
// display function
public static void display(String[] arr)
{
foreach(String a in arr)
Console.WriteLine(a);
}
}
The Original array:
A
D
B
E
C
F
G
After sorting the array using the IComparer:
A
B
C
D
E
F
G
范例2:
// C# program to demonstrate the
// Array.Sort(Array, IComparer) method
using System;
using System.Collections;
class compare : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(x, y);
}
}
class GFG {
// Main Method
public static void Main()
{
// Initializing array.
int[] arr = {10, 1, 9, 8, 3,
4, 6, 5, 2, 7};
// Instantiate the IComparer object
IComparer cmp = new compare();
// Display the original values of the array
Console.WriteLine("The Original array:");
display(arr);
// Sort the entire array by
// using the IComparer object
// "cmp" is the IComparer object
Array.Sort(arr, cmp);
Console.WriteLine("\n\nAfter sorting the "+
"array using the IComparer:");
display(arr);
}
// display function
public static void display(int[] arr)
{
foreach(int a in arr)
Console.Write(a + " ");
}
}
The Original array:
10 1 9 8 3 4 6 5 2 7
After sorting the array using the IComparer:
1 2 3 4 5 6 7 8 9 10
Sort(Array,Array,IComparer)方法
此方法使用指定的IComparer根据第一个数组中的键对一对一维数组对象进行排序。
Syntax: public static void Sort (Array keys, Array items, IComparer comparer);
Parameters:
keys: It is the one-dimensional array that contains the keys to sort.
items: It is the one-dimensional array that contains the items that correspond to each of the keys in the keys array.
comparer: It is the IComparer implementation to use when comparing elements.
例外情况:
- ArgumentNullException:如果键为null。
- RankException:如果keys数组是多维的,或者items数组是多维的。
- ArgumentException:如果项不为null,并且键的长度大于项的长度,或者比较器的实现在排序期间导致错误。
- InvalidOperationException:如果比较器为null。
例子:
// C# program to demonstrate the
// Array.Sort(Array, Array,
// IComparer) method
using System;
using System.Collections;
class comparer : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(x, y);
}
}
class GFG {
// Main Method
public static void Main()
{
// initialize two arrays
String[] arr1 = {"H", "J", "K",
"L", "I", "N", "M"};
String[] arr2 = {"A", "E", "D",
"C", "F", "B", "G"};
// Instantiate the IComparer object
IComparer g = new comparer();
// Display original values of the array.
Console.WriteLine("The original order of "+
"elements in the array:");
Display(arr1, arr2);
// Sort the array using IComparer
// object. "g" is IComparer object
Array.Sort(arr1, arr2, g);
Console.WriteLine("\nAfter sorting :");
Display(arr1, arr2);
}
// Display function
public static void Display(String[] arr1, String[] arr2)
{
for (int i = 0; i < arr1.Length; i++) {
Console.WriteLine(arr1[i] + " : " + arr2[i]);
}
}
}
The original order of elements in the array:
H : A
J : E
K : D
L : C
I : F
N : B
M : G
After sorting :
H : A
I : F
J : E
K : D
L : C
M : G
N : B
Sort(Array,Array)方法
此方法使用每个键的IComparable实现基于第一个Array中的键对一对一维数组对象进行排序。在此,对象中有两个数组,其中一个包含键,另一个包含对应的项。
Syntax: public static void Sort (Array keys, Array items);
Parameters:
keys: It is the one-dimensional array that contains the keys to sort.
items: It is the one-dimensional array that contains the items that correspond to each of the keys in the keys array.
例外情况:
- ArgumentNullException:如果键为null。
- RankException:如果keys数组是多维的,或者items数组是多维的。
- ArgumentException:如果items不为null,并且键的长度大于items的长度。
- InvalidOperationException:如果keys数组中的一个或多个元素未实现IComparable接口。
例子:
// C# program to demonstrate the
// Array.Sort(Array, Array) method
using System;
class GFG {
// Main Method
public static void Main()
{
// initialize two array.
int[] arr1 = {7, 5, 2, 3, 1, 6, 4};
string[] arr2 = {"A", "E", "D",
"C", "F", "B", "G"};
// Display original values of the array.
Console.WriteLine("The original array:");
Display(arr1, arr2);
// Sort the array using two array
// "arr1" is keys array
// "arr2" item array
Array.Sort(arr1, arr2);
Console.WriteLine("\nAfter Sorting :");
Display(arr1, arr2);
}
// Display function
public static void Display(int[] arr1, string[] arr2)
{
for (int i = 0; i < arr1.Length; i++)
{
Console.WriteLine(arr1[i] + " : " + arr2[i]);
}
}
}
The original array:
7 : A
5 : E
2 : D
3 : C
1 : F
6 : B
4 : G
After Sorting :
1 : F
2 : D
3 : C
4 : G
5 : E
6 : B
7 : A
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array.sort?view=netframework-4.7.2