How to Create a Custom Sorter in Sitecore CMS

June 3, 2015

Blog | Development | How to Create a Custom Sorter in Sitecore CMS
How to Create a Custom Sorter in Sitecore CMS

Sitecore offers a number of options for automatically sorting items, such as by name or creation date. These sorting options, while useful, often fall short of content editor needs. Fortunately, developers can easily write custom sorting methods in Sitecore, which helps create a better user experience. Today, we’ll look at creating a custom sorter.

Creating the Items

Note:

This tutorial will use the templates featured in our post How to Extend Templates in Sitecore CMS. Specifically the “Article” template which will be sorted by its Date field.

In /sitecore/content/Example/Home create a new folder named “Sorting Example”.

In the folder create a handful of Articles. Be sure to set their date fields to various different days.

We will be customizing the sorting on the “Sorting Example” folder to automatically sort the articles.

Writing the Code

Create a new C# class named DateFieldComparer. It will extend the Sitecore.Data.Comparers.Comparer abstract class:

public class DateFieldComparer : Comparer
{ }

Note:

You could use any System.Collections.Generic.IComparer class, but Sitecore’s Comparer class includes the base logic to support sorting based on sortorder.

The Comparer class has an abstract DoCompare method that needs to be implemented.

public override int DoCompare(Item item1, Item item2)
{
    // compare item1 to item2
    // return a value less than zero if item1 should come before item2
    // return zero if item1 is equal to item2
    // return a value greater than zero if item1 should come after item2
}

Getting the date value from the items is a matter of accessing the “Date” field and accessing its DateTime value. I have abstracted this logic to a private static method because it needs to be done more than once:

private static DateTime GetDateTime(Item item)
{
    var dateField = (DateField) item.Fields["Date"];
    return dateField != null ? dateField.DateTime : DateTime.MinValue;
}

Once the date fields are accessed, their values can be compared using DateTime’s CompareTo method:

protected override int DoCompare(Item item1, Item item2)
{
    var date1 = GetDateTime(item1);
    var date2 = GetDateTime(item2);

    return date1.CompareTo(date2);
}

All Together Now

The complete code example is:

using System;
using Sitecore.Data.Comparers;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;

namespace GeekHive.Framework.Sitecore.Comparers
{
    public class DateFieldComparer : Comparer
    {
        protected override int DoCompare(Item item1, Item item2)
        {
            var date1 = GetDateTime(item1);
            var date2 = GetDateTime(item2);

            return date1.CompareTo(date2);
        }

        private static DateTime GetDateTime(Item item)
        {
            var dateField = (DateField) item.Fields["Date"];
            return dateField != null ? dateField.DateTime : DateTime.MinValue;
        }
    }
}

Creating the “Child Sorting” Item

Select the /sitecore/system/Settings/Subitems Sorting item.

Insert a new /sitecore/templates/System/Child Sorting item named “Date Field”.

Update the “Type” field to point to the class that will be used for sorting. My class will be named DateFieldComparer in the GeekHive.Framework.Sitecore.Comparers namespace within a DLL named GeekHive.Framework.dll, so my type field will be GeekHive.Framework.Sitecore.Comparers.DateFieldComparer,GeekHive.Framework.

Using the new Sorting Method

Select the /sitecore/content/Example/Home/Sorting Example item again and update its Subitems Sorting to use the new method.

The items will now automatically arrange themselves based on the values in their Date fields.


Thanks for checking in. And, as always, if you have any Sitecore questions or if there’s something you’d like to see us cover, drop us a line!

Tim Leverett

Senior Developer
Tags
  • Sitecore

Recent Work

Check out what else we've been working on