Although the concept of templates is not quite new, the particular implementation of templates done by Generics is quite nice. I love working with them, they have proven to be very useful and flexible. If you have not used them before, you should give it a try.
For this shor example, we will covering one of the useful advantages of the Generic List< >
Custom Sorting
public class Student
{ //For Simplicit purposes these are public
public int age;
public string name;
public string id;
public Student(int _age, string _name, string _id)
{
age = _age;
name = _name;
id = _id;
}
public override string ToString()
{
string s = age + "\n" + name + "\n" + id + "\n";
return s;
}
}
Now we have the class, need to create and instanciate our list of students and add students
//Declare static since this is called from a console application
public static List studentList ;public static void AddStudents()
{
Student student1 = new Student(20, "Bolton", "M");
Student student2 = new Student(20, "Abel", "M");
Student student3 = new Student(21, "Catherine", "1234");
Student student4 = new Student(21, "Catherine", "1111");
Student student5 = new Student(22, "Bart", "M");
studentList.Add(student1);
studentList.Add(student2);
studentList.Add(student3);
studentList.Add(student4);
studentList.Add(student5);
}
Now lets go unto sorting our list, this is the interesting part.
public static int SortStudents(Student leftStudent, Student rightStudent)
{
//sort first by Age
if (leftStudent.age > rightStudent.age)
return 1;
else if (leftStudent.age < rightStudent.age)
return -1;
else //if both students has the same age
{
//sort by name
if (leftStudent.name.CompareTo(rightStudent.name) != 0)
{
return leftStudent.name.CompareTo(rightStudent.name);
}
else //if both students has the same name and age, sort by id
{
if (leftStudent.id.CompareTo(rightStudent.id) != 0)
{
return leftStudent.id.CompareTo(rightStudent.id);
}
else
{
//in case both students to compare have identical values for all three properties
return 0;
}
}
}
}
The signature of this function is going to be similar for any object we would like to store in our List.
We’ll return an static int, and have will have two parameters of the type we are storing in our List; in this particular example since our list is a List of student we have both parameters of types students. Inside the body of the function we can do any comparison that we want to. Keeping in mind that we always need to return 0 in case both objects have the same value for all their properties. And that is, custom sorting of the objects store in any List.
Below the complete code for our console application example
namespace ListCustomSort
{
class Program
{public static List studentList ;
static void Main(string[] args)
{
studentList = new List();
AddStudents();
DisplayStudents();
Console.WriteLine("Sorting students");
studentList.Sort(SortStudents);
DisplayStudents();
}
public static void DisplayStudents()
{
foreach(Student student in studentList)
{
Console.Write(student.ToString());
Console.WriteLine("-------------------");
}
}
public static void AddStudents()
{
Student student1 = new Student(20, "Bolton", "M");
Student student2 = new Student(20, "Abel", "M");
Student student3 = new Student(21, "Catherine", "1234");
Student student4 = new Student(21, "Catherine", "1111");
Student student5 = new Student(22, "Bart", "M");
studentList.Add(student1);
studentList.Add(student2);
studentList.Add(student3);
studentList.Add(student4);
studentList.Add(student5);
}
public static int SortStudents(Student leftStudent, Student rightStudent)
{
//sort first by Age
if (leftStudent.age > rightStudent.age)
return 1;
else if (leftStudent.age < rightStudent.age)
return -1;
else //if both students has the same age
{
//sort by name
if (leftStudent.name.CompareTo(rightStudent.name) != 0)
{
return leftStudent.name.CompareTo(rightStudent.name);
}
else //if both students has the same name and age, sort by id
{
if (leftStudent.id.CompareTo(rightStudent.id) != 0)
{
return leftStudent.id.CompareTo(rightStudent.id);
}
else
{
//in case both students to compare have identical values for all three properties
return 0;
}
}
}
}
}
}