C# Array Sort(Array, Array, IComparer)
Description
Array Sort(Array, Array, IComparer)
sorts a pair of one-dimensional
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.
Syntax
Array.Sort(Array, Array, IComparer)
has the following syntax.
public static void Sort(
Array keys,// www . j ava 2s. c om
Array items,
IComparer comparer
)
Parameters
Array.Sort(Array, Array, IComparer)
has the following parameters.
keys
- The one-dimensional Array that contains the keys to sort.items
- The one-dimensional Array that contains the items that correspond to each of the keys in the keys Array.items
- -or-items
- null to sort only the keys Array.comparer
- The IComparer implementation to use when comparing elements.comparer
- -or-comparer
- null to use the IComparable implementation of each element.
Returns
Array.Sort(Array, Array, IComparer)
method returns
Example
The following code example shows how to sort the values in an Array using the default comparer and a custom comparer that reverses the sort order.
//from ww w . j a v a 2s . c om
using System;
using System.Collections;
class myReverserClass : IComparer {
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
public class SamplesArray {
public static void Main() {
String[] myArr = { "A", "B", "C", "F", "E", "o", "t", "P", "D" };
IComparer myComparer = new myReverserClass();
Array.Sort( myArr, 1, 3 );
Array.Sort( myArr, 1, 3, myComparer );
Array.Sort( myArr );
Array.Sort( myArr, myComparer );
}
}
The code above generates the following result.