Monday, May 5, 2008

Comparison of 2 standard c# objects.

    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:


consoleSign



Reference:



Friday, May 2, 2008

Sorting with help of delegate Comparison.

    When we use Generic class List(T), we can easy to sort it with help of method Sort and delegate Comparison.

Sample.

Definition of class Data:

internal class Data
{
internal int id;
internal string name;
internal Data(int id, string name)
{
this.id = id;
this.name = name;
}
}



Initialization of List<Data>:



List<Data> dataList = new List<Data>();
dataList.Add(new Data(4, "London"));
dataList.Add(new Data(2, "Rome"));
dataList.Add(new Data(3, "New York"));
dataList.Add(new Data(5, "Paris"));
dataList.Add(new Data(1, "Jerusalem"));

DataServices.PrintList("by original order:", dataList);



Sorting dataList by field id:



dataList.Sort(delegate(Data data1, Data data2) { return data1.id - data2.id; });
DataServices.PrintList("by id:", dataList);



Sorting dataList by field name:



dataList.Sort(delegate(Data data1, Data data2) { return String.Compare(data1.name, data2.name); });
DataServices.PrintList("by name:", dataList);



Console output:



consoleComparison



Reference:



List(T).Sort Method



Comparison(T) Generic Delegate



Using IComparable and IComparer to compare objects