I'll show you how to sort and iterate list object in generic
public class Customer
{
public int custId;
public string custName;
public Customer(int id, string name)
{
this.custId = id;
this. custName = name;
}
}
List<Customer> cust = new List<Customer>();
cust.Add(new Customer(1008, "Microsoft"));
cust.Add(new Customer(999, "Google"));
cust.Add(new Customer(879, "Yahoo"));
cust.Add(new Customer(1206, "Amazon"));
cust.Sort(delegate(Customer c1, Customer c2)
{ return c1. custName.CompareTo(c2. custName); });
cust.ForEach(delegate(Customer c)
{ Console.WriteLine(String.Format("{0} {1}", c.custId, c.custName)); });
Sorted list
1206 Amazon
999 Google
1008 Microsoft
879 Yahoo
The above code using Lambda Expressions
//with anonymous method
cust.Sort(delegate(Customer c1, Customerc2)
{ return c1. custName.CompareTo(c2. custName); });
//with Lambda Expressions
cust.Sort((c1,c2)=>c1.custName.CompareTo(c2.custName));
//with anonymous method
cust.ForEach(delegate(Customer c)
{ Console.WriteLine(String.Format("{0} {1}", c.custId, c.custName)); });
//with Lambda Expressions
cust.ForEach(c=> Console.WriteLine(String.Format("{0} {1}", c.custId, c.custName)));