Anonymous Method of C#2.0 can ease Sorting operation in Ascending / Descending order on Generic collection List. One can avoid tedious use of implementing IComparable / IComparer for List. This sample code shows how to use Anonymous methdos in C#.
protected void Page_Load(object sender, EventArgs e) { List customerList = new List(); customerList.Add(new Customer(01111, "Santosh")); customerList.Add(new Customer(01112, "Narayan")); customerList.Add(new Customer(01113, "Poojari")); customerList.Sort( delegate(Customer lhs, Customer rhs) { return lhs.CustomerID.CompareTo(rhs.CustomerID); //OR return rhs.CustomerID.CompareTo(lhs.CustomerID); }); //one can use customerList.Reverse() to reverse the sort order foreach (Customer customer in customerList) Response.Write(customer.CustomerID+" "); } public class Customer { private int m_CustomerID = int.MinValue; private string m_CustomerName = string.Empty; public Customer(int customerId,string customerName) { this.m_CustomerID = customerId; this.m_CustomerName = customerName; } public int CustomerID { get { return m_CustomerID; } set { m_CustomerID = value; } } public string CustomerName { get { return m_CustomerName; } set { m_CustomerName = value; } } }
|
| Author: Vasudevan Deepak Kumar 18 Jul 2007 | Member Level: Diamond Points : 0 |
I would say this tool would better befit in this URL (http://www.dotnetspider.com/tutorials/BestPractices.aspx)
|