Sort the elements in an Array using the specified Comparison in CSharp
Description
The following code shows how to sort the elements in an Array using the specified Comparison.
Example
using System;//w w w .j a v a 2 s. c om
using System.Collections.Generic;
public class Example
{
private static int CompareDinosByLength(string x, string y)
{
if (x == null || y == null){
return 0;
}else{
return -1;
}
}
public static void Main()
{
string[] myValues = {
"2016",
"1999",
null,
null,
"2000",
"2002" };
Array.Sort(myValues, CompareDinosByLength);
foreach(String s in myValues){
Console.WriteLine(s);
}
}
}
The code above generates the following result.