C# Array Sort(T[], Int32, Int32, IComparer)
Description
Array Sort
sorts
the elements in a range of elements in an Array using the specified IComparer
Syntax
Array.Sort<T>(T[], Int32, Int32, IComparer<T>)
has the following syntax.
public static void Sort<T>(
T[] array,/* w ww. j a v a 2 s. com*/
int index,
int length,
IComparer<T> comparer
)
Parameters
Array.Sort<T>(T[], Int32, Int32, IComparer<T>)
has the following parameters.
T
- The type of the elements of the array.array
- The one-dimensional, zero-based Array to sort.index
- The starting index of the range to sort.length
- The number of elements in the range to sort.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<T>(T[], Int32, Int32, IComparer<T>)
method returns
Example
using System;/*from ww w . ja v a 2 s . c o 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[] dinosaurs = {"2016",
"1999",
"2000",
"1234",
"2001",
"2345"};
Array.Sort(dinosaurs, 3, 3, new ReverseComparer());
}
}
The code above generates the following result.