C# Array Sort(TKey[], TValue[], IComparer)
Description
Array Sort(TKey[], TValue[], IComparer)
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 specified IComparer generic interface.
Syntax
Array.Sort(TKey[], TValue[], IComparer)
has the following syntax.
public static void Sort<TKey, TValue>(
TKey[] keys,/* w w w. ja v a 2 s . co m*/
TValue[] items,
IComparer<TKey> comparer
)
Parameters
Array.Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>)
has the following parameters.
TKey
- The type of the elements of the key array.TValue
- The type of the elements of the items array.keys
- The one-dimensional, zero-based Array that contains the keys to sort.items
- The one-dimensional, zero-based Array that contains the items that correspond to the keys in keys, or null to sort only keys.comparer
- The IComparergeneric interface implementation to use when comparing elements, or null to use the IComparable generic interface implementation of each element.
Returns
Array.Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>)
method returns
Example
using System;//from w w w. ja v a 2s .co m
using System.Collections.Generic;
public class ReverseComparer: IComparer<string>
{
public int Compare(string x, string y)
{
// Compare y and x in reverse order.
return y.CompareTo(x);
}
}
public class Example
{
public static void Main()
{
string[] myValues = {"40", "5", "3", "22", "1", "18" };
int[] myValuesizes = { 40, 5, 3, 22, 1, 18 };
Array.Sort(myValues, myValuesizes);
ReverseComparer rc = new ReverseComparer();
Array.Sort(myValues, myValuesizes, rc);
Array.Sort(myValues, myValuesizes, 3, 3);
Array.Sort(myValues, myValuesizes, 3, 3, rc);
}
}
The code above generates the following result.