Array.Sort方法用于对一维数组中的元素进行排序。此方法的重载列表中有17种方法。在这里,我们将讨论以下方法:
- Sort
(TKey [],TValue [],IComparer )方法 - Sort
(TKey [],TValue [],Int32,Int32)方法 - Sort
(TKey [],TValue [],Int32,Int32,IComparer )方法
Sort (TKey [],TValue [],IComparer )方法
此方法使用指定的IComparer; T>通用接口,根据第一个数组中的键对一对数组对象进行排序。在这两个数组中,一个包含键,另一个包含对应的项。
Syntax: public static void Sort
Here, TKey is the type of the elements of the key array and TValue the type of the elements of the items array.
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 the keys in keys.
comparer: It is the IComparer
例外情况:
- ArgumentNullException:如果键为null。
- ArgumentException的:如果项不为空和下界密钥不匹配所述下界物品或物品的不为空和密钥的长度比物品的长度大。
- InvalidOperationException:如果比较器为null
例子:
// C# program to demonstrate the use of
// Array.Sort(TKey[],
// TValue[], IComparer) Method
using System;
using System.Collections.Generic;
class compare : IComparer {
public int Compare(string x, string y)
{
// Compare x to y
return x.CompareTo(y);
}
}
// Driver Class
class GFG {
// Main Method
public static void Main()
{
// Initialize two array
String[] arr1 = { "H", "J", "K",
"L", "I", "N", "M" };
String[] arr2 = { "A", "E", "D",
"C", "F", "B", "G" };
// Instantiate the IComparer object
compare g = new compare();
// Display orginal values of the array
Console.WriteLine("The original order of"
+ " elements in the array:");
Display(arr1, arr2);
// Sort the array
// "arr1" is keys array
// "arr2" is items array
// "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 (TKey [],TValue [],Int32,Int32)方法
此方法用于根据第一个数组中的键对一对数组对象中的元素范围进行排序。在这两个数组中,一个包含键,另一个包含对应的项。
Syntax: public static void Sort
Here, TKey is the type of the elements of the key array and TValue is the type of the elements of the items array.
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 the keys in keys.
comparer: It is the IComparer
index: It is the starting index of the range to sort.
len: It is the number of elements in the range to sort.
例外情况:
- ArgumentNullException:如果键为null。
- ArgumentOutOfRangeException:如果index小于键的下限或len小于零。
- ArgumentException:如果item不为null,并且键的下限不匹配item的下限,或者items不为null,并且len的长度大于item或索引的长度,并且len不指定in的有效范围keysArray或items不为null,并且index和len没有在itemsArray中指定有效范围。
- InvalidOperationException:当keysArray中的一个或多个元素未实现IComparable
通用接口时。
例子:
// C# program to demonstrate the use of
// Array.Sort(TKey[], TValue[],
// Int32, Int32) Method
using System;
// Driver Class
class GFG {
// Main Method
public static void Main()
{
// Initialize two array
String[] arr1 = {"H", "J", "K",
"L", "I", "M", "N"};
String[] arr2 = {"A", "E", "D",
"C", "F", "B", "G"};
// Display orginal values of the array
Console.WriteLine("The original order of"
+ " elements in the array:");
Display(arr1, arr2);
// Sort the array
// "arr1" is keys array
// "arr2" is items array
// start index 1
// rang upto index 5
Array.Sort(arr1, arr2, 1, 5);
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
M : B
N : G
After Sorting:
H : A
I : F
J : E
K : D
L : C
M : B
N : G
Sort (TKey [],TValue [],Int32,Int32,IComparer )方法
此方法用于使用指定的IComparer
Syntax: public static void Sort
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 the keys in keys.
comparer: It is the IComparer
index: It is the starting index of the range to sort.
len: It is the number of elements in the range to sort.
comparer: It is the IComparer
例外情况:
- ArgumentNullException:如果键为null。
- ArgumentOutOfRangeException:如果索引小于键的下限,或者len小于零。
- ArgumentException:如果items不为null,并且键的下限不匹配item的下限,或者items不为null,并且键的len大于item或index的长度,并且len在s中不指定有效范围keysArray或items不为null,并且index和len没有在itemsArray中指定有效范围,或者比较器的实现在排序过程中导致了错误。
- InvalidOperationException:当keysArray中的一个或多个元素未实现IComparable
通用接口时。
例子:
// C# program to demonstrate the use of
// Array.Sort(TKey[], TValue[],
// Int32, Int32, IComparer) Method
using System;
using System.Collections.Generic;
class compare : IComparer {
public int Compare(string x, string y)
{
// Compare x to y
return x.CompareTo(y);
}
}
// Driver Class
class GFG {
// Main Method
public static void Main()
{
// Initialize two array
String[] arr1 = {"H", "J", "K",
"L", "I", "M", "N"};
String[] arr2 = {"A", "E", "D",
"C", "F", "B", "G"};
// Instantiate the IComparer object
compare g = new compare();
// Display orginal values of the array
Console.WriteLine("The original order of"
+ " elements in the array:");
Display(arr1, arr2);
// Sort the array
// "arr1" is keys array
// "arr2" is items array
// "g" is IComparer object
// start index 1
// rang upto index 5
Array.Sort(arr1, arr2, 1, 5, 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
M : B
N : G
After Sorting:
H : A
I : F
J : E
K : D
L : C
M : B
N : G
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array.sort?view=netframework-4.7.2