Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>) Method

This method sorts a pair of array objects based on the keys in the first array using the specified IComparer;T> generic interface. Here in the 2 arrays one contains the keys and the other contains the corresponding items.

Syntax: public static void Sort<TKey, TValue> (TKey[] keys, TValue[] items, IComparer comparer);
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<T> generic interface implementation to use when comparing elements.

Exceptions:

ArgumentNullException: If the keys is null.

ArgumentException: If the items is not null and the lower bound of keys does not match the lower bound of items or items is not null and the length of keys is greater than the length of items.

Sort<TKey, TValue>(TKey[], TValue[], Int32, Int32) Method

This method is used to sort a range of elements in a pair of array objects based on the keys in the first array. Here in the 2 arrays one contains the keys and the other contains the corresponding items.

Syntax: public static void Sort<TKey, TValue> (TKey[] keys, TValue[] items, IComparer comparer, int index, int len);
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<T> generic interface implementation to use when comparing elements.index: It is the starting index of the range to sort.len: It is the number of elements in the range to sort.

Exceptions:

ArgumentNullException: If the keys is null.

ArgumentOutOfRangeException: If index is less than the lower bound of keys or len is less than zero.

ArgumentException: If the items is not null and the lower bound of keys does not match the lower bound of items or items is not null and the len of keys is greater than the length of items or index and len do not specify a valid range in the keysArray or items is not null and index and len do not specify a valid range in the itemsArray.

InvalidOperationException: When one or more elements in the keysArray do not implement the IComparable<T> generic interface.

This method is used to sort a range of elements in a pair of array objects based on the keys in the first array using the specified IComparer<T> generic interface. Here in the 2 arrays one contains the keys and the other contains the corresponding 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 the keys in keys.comparer: It is the IComparer<T> generic interface implementation to use when comparing elements.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<T> generic interface implementation to use when comparing elements.

Exceptions:

ArgumentNullException: If the keys is null.

ArgumentOutOfRangeException: If the index is less than the lower bound of keys or len is less than zero.

ArgumentException: If items is not null and the lower bound of keys does not match the lower bound of items or items is not null and the len of keys is greater than the length of items or index and len do not specify a valid range in the keysArray or items is not null and index and len do not specify a valid range in the itemsArray or the implementation of comparer caused an error during the sort.

InvalidOperationException: When one or more elements in the keysArray do not implement the IComparable<T> generic interface.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.