C# Array Sort(Array, Int32, Int32)
Description
Array Sort(Array, Int32, Int32)
sorts the elements in
a range of elements in a one-dimensional Array using the IComparable implementation
of each element of the Array.
Syntax
Array.Sort(Array, Int32, Int32)
has the following syntax.
public static void Sort(
Array array,// w ww. j av a 2 s. c om
int index,
int length
)
Parameters
Array.Sort(Array, Int32, Int32)
has the following parameters.
array
- The one-dimensional Array to sort.index
- The starting index of the range to sort.length
- The number of elements in the range to sort.
Returns
Array.Sort(Array, Int32, Int32)
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.
// ww w .jav a 2 s . c o m
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 = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
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.