May 19, 2020 07:54 by
Peter
One of the most used functionalities in our C# classes is the ability to sort the different instances of a class. This is mostly done when we create a generic List of our class type and next, we want to sort our class based on some condition. Today, we will see how this is done in our C# classes
Creating the Class
We will create a .NET core console application using Visual Studio 2019 Community edition as below,
Next, we create a Student Class inside the “Program.cs” file as below,
public class Student : IComparable<Student>
{
public int ID { get; set; }
public string Name { get; set; }
public string Program { get; set; }
public int Year { get; set; }
public float GPA { get; set; }
public int CompareTo(Student otherItem)
{
var status = (this.GPA > otherItem.GPA) ? 1 : ((this.GPA == otherItem.GPA) ? 0 : -1);
return status;
}
}
Let us look at the code in detail. To make the class sortable we need to implement the IComparable<T> interface. In this interface we need to code the “CompareTo” function. In this function another instance of the same class is passed in and then we compare the two to order them. We base this on the GPA field and hence when we sort a List<Student> list, we will get the class items sorted by GPA in ascending order. Note that if the return value is zero, this means that the classes are equal in sort order, if the return value is 1 (positive number), then the current class is higher in the sort and if the value is -1 (negative number) then the input class is higher in the sort order.
To test this out, we write the following code in the Main function as below,
class Program
{
static void Main(string[] args)
{
var students = new List<Student>();
var studentA = new Student() { ID = 1, Name = "John Doe", Program = "BCS", Year = 2020, GPA = 2.75F };
var studentB = new Student() { ID = 1, Name = "Jane Doe", Program = "BCS", Year = 2020, GPA = 3.4F };
var studentC = new Student() { ID = 1, Name = "Mary Jane", Program = "BCS", Year = 2020, GPA = 2.71F };
students.Add(studentA);
students.Add(studentB);
students.Add(studentC);
Console.WriteLine($"Items not in any order");
foreach(var student in students)
{
Console.WriteLine($"Student ID: {student.ID}, Name: {student.Name}, GPA: {student.GPA}");
}
Console.WriteLine();
students.Sort();
Console.WriteLine($"Items in ascending order");
foreach (var student in students)
{
Console.WriteLine($"Student ID: {student.ID}, Name: {student.Name}, GPA: {student.GPA}");
}
Console.WriteLine();
students.Sort(new DescendingComparer());
Console.WriteLine($"Items in descending order");
foreach (var student in students)
{
Console.WriteLine($"Student ID: {student.ID}, Name: {student.Name}, GPA: {student.GPA}");
}
Console.ReadKey();
}
}
public class DescendingComparer : IComparer<Student>
{
public int Compare(Student a, Student b)
{
var status = (a.GPA > b.GPA) ? -1 : ((a.GPA == b.GPA) ? 0 : 1);
return status;
}
}
In the first case, we print the list of student classes as entered in the list. Then, we sort the list which will list the students in ascending order based on the GPA field. Finally, if we want to reverse the order and sort the student classes in descending order by GPA field we create a new DescendingComparer class which implements the IComparer<Student> interface and sort the records in the reverse order of what we did before.