Array.Sort方法用于对一维数组中的元素进行排序。此方法的重载列表中有17种方法。在这里,我们将讨论以下方法:
- Sort(Array,Int32,Int32,IComparer)方法
- Sort(Array,Array,Int32,Int32,IComparer)方法
- Sort(Array,Int32,Int32)方法
- Sort(Array,Array,Int32,Int32)方法
Sort(Array,Int32,Int32,IComparer)方法
此方法使用指定的IComparer对一维数组中的范围内的元素进行排序。
Syntax: public static void Sort (Array arr, int start, int len, IComparer comparer);
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 implementation to use when comparing elements or null to use the IComparable implementation of each element.
例外情况:
- ArgumentNullException:如果数组为null。
- RankException:如果数组是多维的。
- ArgumentOutOfRangeException-如果起始索引小于array的下限,或者len小于零。
- ArgumentException:如果开始索引和len未在数组中指定有效范围,或者如果comparer的实现在排序期间导致错误。
- InvalidOperationException:如果comparer为null,并且数组中的一个或多个元素不实现IComparable接口。
例子:
// C# program to demonstrate the use
// of Array.Sort(Array, Int32, Int32,
// 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 a array.
string[] arr = {"ABC", "GHI", "JKL",
"DEF", "MNO", "XYZ"};
// Instantiate the reverse comparer.
IComparer g = new comparer();
// Display original values of the array.
Console.WriteLine("The original order of "+
"elements in the array:");
Display(arr);
// Sort a section of the array
// using the IComparer object
// sorting happens in the range
// of index 1 to 4
// "g" is IComparer object
Array.Sort(arr, 1, 4, g);
Console.WriteLine("\nAfter sorting in a range of"+
" index 1 to 4 using the IComparer object:");
Display(arr);
}
// Display function
public static void Display(string[] arr)
{
for (int i = arr.GetLowerBound(0);
i <= arr.GetUpperBound(0); i++) {
Console.WriteLine(arr[i]);
}
}
}
The original order of elements in the array:
ABC
GHI
JKL
DEF
MNO
XYZ
After sorting in a range of index 1 to 4 using the IComparer object:
ABC
DEF
GHI
JKL
MNO
XYZ
Sort(Array,Array,Int32,Int32,IComparer)方法
此方法使用指定的IComparer根据第一个Array中的键对一对一维数组对象中的元素范围进行排序。这里的对象包含键和相应的项目。
Syntax: public static void Sort (Array key, Array items, int start, int len, IComparer comparer);
Parameters:
key: It is the one-dimensional array which contains the keys to sort.
items: It is the one-dimensional array which contains the items that correspond to each of the keys in the keysArray(previous array).
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 implementation to use when comparing elements or null to use the IComparable implementation of each element.
例外情况:
- ArgumentNullException:如果键为null。
- RankException:如果keysArray是多维的。
- ArgumentOutOfRangeException:如果起始索引小于键的下限,或者len小于零。
- ArgumentException:
- 如果项目不为null,并且键的下限与项目的下限不匹配,或者
- 如果项目不为空,并且键的len大于项目的长度
- 如果起始索引和len在keysArray中未指定有效范围。
- 如果items不为null,并且开始index和len不在itemsArray中指定有效范围。
- 如果比较器的实现在排序过程中引起了错误。
- InvalidOperationException:如果比较器为null。
例子:
// C# program to demonstrate the use
// of Array.Sort(Array, Array, Int32,
// Int32, IComparer) Method
using System;
using System.Collections;
class comparer : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(y, x);
}
}
// Driver Class
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 reverse comparer.
IComparer g = new comparer();
// Display original values of the array.
Console.WriteLine("The original order of "+
"elements in the array:");
Display(arr1, arr2);
// Sort a section of the array
// using the IComparer object
// sorting happens in the range
// of index 1 to 4
// "g" is IComparer object
Array.Sort(arr1, arr2, 1, 4, g);
Console.WriteLine("\nAfter sorting in a "+
"range of index 1 to 4 :");
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 in a range of index 1 to 4 :
H : A
L : C
K : D
J : E
I : F
N : B
M : G
Sort(Array,Int32,Int32)方法
此方法使用Array中每个元素的IComparable实现对一维数组中某个范围内的元素进行排序。
Syntax: public static void Sort (Array arr, int start, int len);
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.
例外情况:
- ArgumentNullException:如果数组为null。
- RankException:如果数组是多维的。
- ArgumentOutOfRangeException:如果开始索引小于数组的下限,或者len小于零。
- ArgumentException:如果起始索引和len在数组中未指定有效范围。
- InvalidOperationException:如果数组中的一个或多个元素未实现IComparable接口。
例子:
// C# program to demonstrate the use of
// Array.Sort(Array, Int32, Int32) method
using System;
using System.Collections;
class GFG {
// Main Method
public static void Main()
{
// initialize a array.
string[] arr = {"ABC", "GHI", "JKL",
"DEF", "MNO", "XYZ"};
// Display original values of the array.
Console.WriteLine("The original order of"+
" elements in the array:");
Display(arr);
// sorting happens in the
// range of index 1 to 4
Array.Sort(arr, 1, 4);
Console.WriteLine("\nAfter sorting in a range of "+
"index 1 to 4 using the IComparer object:");
Display(arr);
}
// Display function
public static void Display(string[] arr)
{
for (int i = arr.GetLowerBound(0);
i <= arr.GetUpperBound(0); i++) {
Console.WriteLine(arr[i]);
}
}
}
The original order of elements in the array:
ABC
GHI
JKL
DEF
MNO
XYZ
After sorting in a range of index 1 to 4 using the IComparer object:
ABC
DEF
GHI
JKL
MNO
XYZ
Sort(Array,Array,Int32,Int32)方法
此方法使用指定的IComparer根据第一个Array中的键对一对一维Array对象中的元素范围进行排序。这里的对象包含键和相应的项目。
Syntax: public static void Sort (Array keys, Array items, int start, int len);
Parameters:
key: It is the one-dimensional array which contains the keys to sort.
items: It is the one-dimensional array which contains the items that correspond to each of the keys in the keysArray(previous array).
start: It is the starting index of the range to sort.
len: It is the number of elements in the range to sort.
例外情况:
- ArgumentNullException:如果键为null。
- RankException:如果keysArray是多维的,或者itemsArray是多维的。
- ArgumentOutOfRangeException:如果起始索引小于键的下限,或者len小于零。
- ArgumentException:
- 如果item不为null,并且key的len大于items的长度。
- 如果起始索引和len在keysArray中未指定有效范围。
- 如果items不为null,并且开始index和len不在itemsArray中指定有效范围。
- 如果比较器的实现在排序过程中引起了错误。
- InvalidOperationException:如果keysArray中的一个或多个元素未实现IComparable接口。
例子:
// C# program to demonstrate the use of
// Array.Sort(Array, Int32, Int32,
// IComparer) method
using System;
using System.Collections;
class comparer : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(y, x);
}
}
// Driver Class
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"};
// Display original values of the array.
Console.WriteLine("The original order of elements in the array:");
Display(arr1, arr2);
// sorting happens in the range of index 1 to 4
Array.Sort(arr1, arr2, 1, 4);
Console.WriteLine("\nAfter sorting in a "+
"range of index 1 to 4 :");
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 in a range of index 1 to 4 :
H : A
I : F
J : E
K : D
L : C
N : B
M : G
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array.sort?view=netframework-4.7.2