Array.Sort方法用于对一维数组中的元素进行排序。此方法的重载列表中有17种方法。在这里,我们将讨论以下方法:
- Sort(Array)方法
- Sort
(T [],Int32,Int32,IComparer )方法 - Sort
(TKey [],TValue [])方法
Sort(Array)方法
此方法使用Array中每个元素的IComparable实现对整个一维数组中的元素进行排序。
Syntax: public static void Sort (Array arr);
Parameter:
arr: It is the one-dimensional array which is to be sorted.
例外情况:
- ArgumentNullException:如果数组为null。
- RankException:如果数组是多维的。
- InvalidOperationException:如果数组中的一个或多个元素未实现IComparable接口。
例子:
// C# program to demonstrate the
// Array.Sort(Array) method
using System;
class GFG {
// Main Method
public static void Main()
{
// Initialize two array.
string[] arr = {"A", "E", "D",
"C", "F", "B", "G"};
// Display original values of the array
Console.WriteLine("The original array:");
Display(arr);
// Sort the array using two array
Array.Sort(arr);
Console.WriteLine("\n\nAfter sorting :");
Display(arr);
}
// Display function
public static void Display(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
}
The original array:
A E D C F B G
After sorting :
A B C D E F G
Sort (T [],Int32,Int32,IComparer )方法
此方法使用指定的IComparer
Syntax: public static void Sort
Here T is the type of the elements of the array.
Parameters:
arr: It is the one-dimensional array to sort.
start: 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:如果起始索引和长度len在数组中未指定有效范围,或者比较器的实现在排序期间导致错误。
- InvalidOperationException:如果比较器为null。
例子:
// C# program to demonstrate the use of
// Sort(T[], 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);
}
}
class GFG {
// Main Method
public static void Main()
{
// Initializing array
String[] arr = {"A", "D", "B",
"E", "C", "F", "G"};
// Instantiate the IComparer object
compare cmp = new compare();
// Display the original values of the array
Console.WriteLine("The original array:");
display(arr);
// sorting range is index 1 to 4
// "cmp" is the IComparer obeject
Array.Sort(arr, 1, 4, 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
Sort (TKey [],TValue [])方法
此方法使用每个键的IComparable
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.
例外情况:
- ArgumentNullException:如果键为null。
- ArgumentException:如果items不为null且键的下限不匹配items的下限,或者items不为null且键的长度大于items的长度。
- InvalidOperationException:如果keys数组中的一个或多个元素未实现IComparable
通用接口。
例子:
// C# program to demonstrate the use of
// Array.Sort(TKey[], TValue[])
// 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