C# Array Sort(T[], Comparison)
Description
Array Sort
sorts the
elements in an Array using the specified Comparison
Syntax
Array.Sort<T>(T[], Comparison<T>)
has the following syntax.
public static void Sort<T>(
T[] array,
Comparison<T> comparison
)
Parameters
Array.Sort<T>(T[], Comparison<T>)
has the following parameters.
T
- The type of the elements of the array.array
- The one-dimensional, zero-based Array to sortcomparison
- The Comparisonto use when comparing elements.
Returns
Array.Sort<T>(T[], Comparison<T>)
method returns
Example
using System;// w w w . ja v a 2 s . c o m
using System.Collections.Generic;
public class Example
{
private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
return -1;
}
public static void Main()
{
string[] myValues = {
"2016",
"1999",
null,
null,
"2000",
"2002" };
Array.Sort(myValues, CompareDinosByLength);
}
}
The code above generates the following result.