![]() |
VOOZH | about |
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:
This method sorts the elements in an entire one-dimensional array using the IComparable implementation of each element of the Array.
Syntax: public static void Sort (Array arr);
Parameter:
arr: It is the one-dimensional array which is to be sorted.
Exceptions:
Example:
The original array: A E D C F B G After sorting : A B C D E F G
This method sorts the elements in a range of elements in an Array using the specified IComparer<T> generic interface.
Syntax: public static void Sort<T> (T[] arr, int start, int len, IComparer<T> comparer);
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<T> generic interface implementation to use when comparing elements.
Exceptions:
Example:
The original array: A D B E C F G After sorting the array using the IComparer: A B C D E F G
This method sorts a pair of Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable<T> generic interface implementation of each key.
Syntax: public static void Sort<TKey, TValue>(TKey[] keys, TValue[] items);
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.
Exceptions:
Example:
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