C# Array Sort(TKey[], TValue[])
Description
Array Sort(TKey[], TValue[])
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
Syntax
Array.Sort<TKey, TValue>(TKey[], TValue[])
has the following syntax.
public static void Sort<TKey, TValue>(
TKey[] keys,
TValue[] items
)
Parameters
Array.Sort<TKey, TValue>(TKey[], TValue[])
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.
Returns
Array.Sort<TKey, TValue>(TKey[], TValue[])
method returns
Example
using System;// w w w . ja v a2s .com
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.