Sorting with a Comparison Delegate

I’ve just seen the awesome power of delegates, I can’t believe that I’ve never used them all that much before, mostly because I’ve been able to take advantage of using SQL or CAML to do my ordering, hey adding an ORDER BY or <OrderBy> clause is dead easy ;-).

Last night I was in the situation where I was composing a List<T> over n calls to the database and need to order my objects. Now in this case my business object have a few properties, actually they are WCF message objects, and I need to use a flexible ordering scheme as I know of at least two types of sort that are required at this stage. Enter delegate based sorting.

A quick look at the MSDN docs and I’m away.

Business Object:

 [DataContract]
 public class StudentSubmissionActivity
 {
     private string _courseCode;
     private string _description;
     private DateTime _dueDate;

     [DataMember]
     public DateTime DueDate
     {
         get { return _dueDate; }
         set { _dueDate = value; }
     }

     [DataMember]
     public string Description
     {
         get { return _description; }
         set { _description = value; }
     }

     [DataMember]
     public string CourseCode
     {
         get { return _courseCode; }
         set { _courseCode = value; }
     }
 }

CompareByDescription:

public static int CompareByDescription(StudentSubmissionActivity x, StudentSubmissionActivity y)
{
    if (null == x.Description)
    {
        if (null == y.Description)
        {
            // Both the same so return 0
            return 0;
        }
        //y.Description is non null therefore greater, return -1
        return -1;
    }
    else
    {
        if (null == y.Description)
        {
            // x.Description is non-null, therefore greater, return 1
            return 1;
        }
        // both are non-null do the comparison
        return x.Description.CompareTo(y.Description);

    }
}

As strings are nullable we need to do a bunch of null checking before we can call the native string.Compare method.

CompareByDueDate:

public static int CompareByDueDate(StudentSubmissionActivity x, StudentSubmissionActivity y)
{
    return x.DueDate.CompareTo(y.DueDate);
}

Here due to the fact that I’ve used standard DateTime objects I can skip the null checking and just use the native DateTime.Compare method straight off, however if I’d used the nullable DateTime? type then the null checking pattern used in the CompareByDescription method would be necessary.

Now using these comparison methods is dead easy!

Sample Usage:

public List<StudentSubmissionActivity> LoadSortedSubmissionActivities(List<string> courseCodes, StudentSubmissionActivitySorting sortOption)
{
    List<StudentSubmissionActivity> results = new List<StudentSubmissionActivity>();
    foreach (string courseCode in courseCodes)           
    {
                        DataAccess.StudentSubmissionActivity.LoadIntoListForCourse(results, courseCode);
    }

    if (StudentSubmissionActivitySorting.DateSort == sortOption)
    {
        results.Sort(CompareByDueDate);
    }
    else if (StudentSubmissionActivitySorting.DescriptionSort == sortOption)
    {
        results.Sort(CompareByDescription);
    }

    return results;
}
Advertisements
This entry was posted in Development. Bookmark the permalink.