We can create our collection with generic
List<T>, where T can be our BO, which can be strongly typed. For example,
List<Employee> will allow only Employee instance to be added to the list.
Moving forward we will see how to implement sort to using generic list Sort
overload methods in this article.
Things to Consider:
To make things easily understandable, I will use Customer
object with Name and Age attributes as seen in Listing 1.
Listing 1 - Customer BO
class Customer
{
private int _Age;
private string _Name;
public int Age
{
get { return _Age; }
set { _Age = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
So the Generic list that can take Cutomer object can be
defined as,
Listing 2 - Customer BO Generic List
List<Customer> cuslist = new
List<Customer>();
How to sort a Generic List (List<T>)?
To sort the collection, generic list provides sort
method with 4 overloads,
Listing 2 - Generic List Sort Methods
List.Sort ()
List.Sort (Generic Comparison)
List.Sort (Generic IComparer)
List.Sort (Int32, Int32, Generic IComparer)
In the coming sections of this article we will see the
implementation of sort in generic list using the above 4 overloads.
1st Overload - List.Sort ()
This method will sort the members of the list using the
default comparer that is implemented in the object. I would like to sort based
on the name in Customers List.
To make this method to work Customer BO should inherit
IComparable<Customer> and implement public int CompareTo(Customer cus)
method. So our final BO will look like,
Listing 3 - List.Sort () Implementation
class Customer : IComparable<Customer>
{
//Members
//1st Overload
public int CompareTo(Customer cus)
{
return this.Name.CompareTo(cus.Name);
}
}
Consider,
Listing 4 - List creation and Sorting
List<Customer> cuslist1 = new
List<Customer>();
Customer cus1 = new Customer();
cus1.Name = "Fatima";
cus1.Age = 23;
cuslist1.Add(cus1);
Customer cus2 = new Customer();
cus2.Name = "Evangeline";
cus2.Age = 24;
cuslist1.Add(cus2);
Customer cus3 = new Customer();
cus3.Name = "Damien";
cus3.Age = 27;
cuslist1.Add(cus3);
Customer cus4 = new Customer();
cus4.Name = "Cameroon";
cus4.Age = 21;
cuslist1.Add(cus4);
Customer cus5 = new Customer();
cus5.Name = "Babu";
cus5.Age = 20;
cuslist1.Add(cus5);
cuslist1.Sort();
foreach (Customer cus in cuslist1)
{
Console.WriteLine(cus.Name + " " +
cus.Age);
}
The out put of the above code will be,
OUTPUT:
Babu 20
Cameroon 21
Damien 27
Evangeline 24
Fatima 23
2nd Overload - List.Sort (Generic Comparison)
The argument “Generic Comparison” is the
Comparsion<T> delegate called Comparison Generic Delegate.
To understand the implementation of this overload method
we should understand the generic comparison delegate first. The signature of the
delegate is,
Listing 5 - List.Sort (Generic Comparison) syntax
public delegate int Comparison<T> (
T x,
T y
)
To implement this sort method we should define a method
with the above signature that compares the 2 objects and returns an integer
value. Usage of this generic delegate to sort will prevent the need to inherit
the BO with IComparable<T> interface. Refer msdn about the delegate here.
Consider, Customer BO in the section Things to
Consider.
Here I am going to implement 2 sort methods with Generic
Comparison Delegate’s signature, one for sort by name and the other for sort by
Age. Here comes the implementation.
So our final BO will look like,
Listing 6 - Customer BO with Generic Comparison delegate
class Customer
{
//Members
public static int CompareCustomerName(Customer c1,
Customer c2)
{
return c1.Name.CompareTo(c2.Name);
}
public static int CompareCustomerAge(Customer c1,
Customer c2)
{
return c1.Age.CompareTo(c2.Age);
}
}
How to use it?
To use the List.Sort(Generic Comparison) create a
instance of Comparison<Customer> delegate and register it with
CompareCustomerName and CompareCustomerAge method like below,
Listing 7 - List creation and Sorting
List<Customer> cuslist2 = new
List<Customer>();
cus1 = new Customer();
cus1.Name = "Tom";
cus1.Age = 23;
cuslist2.Add(cus1);
cus2 = new Customer();
cus2.Name = "Sanjay";
cus2.Age = 24;
cuslist2.Add(cus2);
cus3 = new Customer();
cus3.Name = "Mathew";
cus3.Age = 27;
cuslist2.Add(cus3);
cus4 = new Customer();
cus4.Name = "Nguyen";
cus4.Age = 21;
cuslist2.Add(cus4);
cus5 = new Customer();
cus5.Name = "Peter";
cus5.Age = 20;
cuslist2.Add(cus5);
Console.WriteLine("\nSort Based on Name");
Comparison<Customer> compnamedel = new
Comparison<Customer>(Customer.CompareCustomerName);
cuslist2.Sort(compnamedel);
foreach (Customer cus in cuslist2)
{
Console.WriteLine(cus.Name + " " +
cus.Age);
}
Console.WriteLine("\nSort Based on Age");
Comparison<Customer> compagedel = new
Comparison<Customer>(Customer.CompareCustomerAge);
cuslist2.Sort(compagedel);
foreach (Customer cus in cuslist2)
{
Console.WriteLine(cus.Name + " " +
cus.Age);
}
The output will be,
OUTPUT:
Sort Based on Name
Mathew 27
Nguyen 21
Peter 20
Sanjay 24
Tom 23
Sort Based on Age
Peter 20
Nguyen 21
Tom 23
Sanjay 24
Mathew 27
|