To sort array (or list) of a objects, we need compare 2 objects and define number:
0 - when 2 objects are same;
+1 - when a 1st object more than 2nd;
-1 - when a 1st object less than 2nd.
Samples:
Sign of number:
int x;
Console.WriteLine(Environment.NewLine + "int x");
Console.WriteLine("Math.Sign({0}) = {1}", x = 10, Math.Sign(x));
Console.WriteLine("Math.Sign({0}) = {1}", x = -10, Math.Sign(x));
Console.WriteLine("Math.Sign({0}) = {1}", x = 0, Math.Sign(x));
Compare 2 strings:
string s1;
string s2;
bool ignoreCase;
Console.WriteLine();
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "a", s2 = "b", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "b", s2 = "a", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "a", s2 = "a", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "aa", s2 = "b", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "bb", s2 = "a", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "A", s2 = "b", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\") = {2}", s1 = "B", s2 = "a", String.Compare(s1, s2));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\", {2}) = {3}", s1 = "a", s2 = "A", ignoreCase = false, String.Compare(s1, s2, ignoreCase));
Console.WriteLine("String.Compare(\"{0}\", \"{1}\", {2}) = {3}", s1 = "a", s2 = "A", ignoreCase = true, String.Compare(s1, s2, ignoreCase));
Compare 2 DateTime objects:
DateTime d1;
DateTime d2;
Console.WriteLine();
Console.WriteLine("DateTime.Compare({0}, {1}) = {2}", d1 = new DateTime(2007, 10, 1), d2 = new DateTime(2007, 10, 1), DateTime.Compare(d1, d2));
Console.WriteLine("DateTime.Compare({0}, {1}) = {2}", d1 = new DateTime(2006, 10, 1), d2 = new DateTime(2007, 10, 1), DateTime.Compare(d1, d2));
Console.WriteLine("DateTime.Compare({0}, {1}) = {2}", d1 = new DateTime(2008, 10, 1), d2 = new DateTime(2007, 10, 1), DateTime.Compare(d1, d2));
Console output:
Reference: