Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods:
- Sort(Array, IComparer) Method
- Sort(Array, Array, IComparer) Method
- Sort(Array, Array) Method
Sort(Array, IComparer) Method
This method sorts the elements in a one-dimensional array using a specified
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.
Exceptions:
- ArgumentNullException: If the array arr is null.
- RankException: If the array arr is multidimensional.
- InvalidOperationException: If the comparer is null.
- ArgumentException: If the implementation of comparer caused an error during the sort.
Example 1:
Output:
The Original array:
A
D
B
E
C
F
G
After sorting the array using the IComparer:
A
B
C
D
E
F
G
Example 2:
Output:
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) Method
This method sorts a pair of one-dimensional array objects based on the
keys in the first array using the specified
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.
Exceptions:
- ArgumentNullException: If the keys is null.
- RankException: If the keys array is multidimensional or the items array is multidimensional.
- ArgumentException: If the items is not null and the length of keys is greater than the length of items or the implementation of comparer caused an error during the sort.
- InvalidOperationException: If the comparer is null.
Example:
Output:
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) Method
This method sorts a pair of one-dimensional array objects based on the
keys in the first Array using the
IComparable implementation of each key. Here, in the objects there are two array in which one contains the keys and the other contains the corresponding items.
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.
Exceptions:
- ArgumentNullException: If the keys is null.
- RankException: If the keys array is multidimensional or the items array is multidimensional.
- ArgumentException: If the items is not null and the length of keys is greater than the length of items.
- InvalidOperationException: If the one or more elements in the keys array do not implement the IComparable interface.
Example: