Sorting Sitecore Items

One of the most unknown but very useful feature in Sitecore is the Subitem Sorting. Often there’s a need to sort a group of Items in a particular order. Either to create a good navigation or to help the content editor find their content.

So let’s have a look how this functionality is working. I’ve installed an empty Sitecore 6.1 installation and created a couple of sample items in a random order.

Sitecore tree with random items
Now I want to sort my items in a less random order. Let’s order them on Display Name. To do so, I’ve to go to my Home and click on the Sorting Command:
Sorting command in Home tab
A new windows will appear with a list of the default Sitecore Sorting settings:

In this case I select ‘Display Name’. The box below will give me an immediate preview how the new order of the Subitems looks:

There are plenty other options to choose, including Standard Values.
When I click on ‘Ok’, the setting will be applied and my content tree will refresh:
To reflect the change in the front-end, you’ve to publish the Home item including subitems (or execute an Incremental Publish).
Note: As always, it’s a best practice to setup Subitem Sorting on the Standard values of a template. That will make sure that all the items based on the Template will reflect the same sort order. For individual cases, you can set it up on an individual item.

To make sure that this example works fine, I’ve made sure that the name of the item is also inserted in the Title field of my Item:

imageNow I want to create a custom Sorter which sorts my items rather on Title than on Display Name. The first thing I’ve to do is to write a small class:

   1: using Sitecore.Data.Comparers;

   2: using Sitecore.Data.Items;


   4: public class YetAnotherComparer1 : Comparer

   5: {

   6:     protected override int DoCompare(Item item1, Item item2)

   7:     {

   8:         string x = item1["title"];

   9:         string y = item2["title"];


  11:         return x.CompareTo(y);

  12:     }

  13: }

The class implements Sitecore.Data.Comparers.Comparer. This base class forces you to implement the DoCompare method. Basically, this method reads both title values and compares it using the .NET built-in string.CompareTo().

Note: I’m using the indexer([“fieldname”]) to retrieve fields. This will never return a null, only a string.Empty when the field doesn’t exist or is empty.

Afterwards, we’ve to register the class in Sitecore. We can create an item of the template ‘Child Sorting’ (/sitecore/templates/System/Child Sorting) in /sitecore/system/settings/Subitems Sorting:

imageNow I’m able to select my item from the sorting dialog:

imageAnd from now on, my items will get sorted by title-value.

Of course this is just an example. You can think about different sorters and also use them in the front-end.

Sitecore’s VP and Founder Lars Fløe Nielsen will follow up on this post with more technical details and how to use Sitecore Sorters in the frond-end. I’ll update this post once his second article is published.

8 thoughts on “Sorting Sitecore Items”

  1. Great and useful post for sure. I recently wrote one that sorts items by date and another by date descending. I especially like how easy it is to simply plug in the custom sorter.

  2. For our website we would like to have the following:

    1. Have an initial sort order which is applied to new children-items of an item
    2. Have the user be able to re-order some of the chlidren inside the content editor
    3. Retrieve the children in the same order as they have in Sitecore, to be used in a .NET web-control

    Is that possible? My initial tests show that it is not. When I order the children using ‘Up’, ‘Down’, ‘First’ and ‘Last’ inside the content editor and fetch these children directly using Item.Children, they do not have the same order as I specified.

    So, the main issue of my question is the one stated in step 3 (how to retrieve items in the order they have in Sitecore) and I guess it is not exactly a sorting-question, but I hope you can help anyway 🙂

  3. @Esben, I know I’m replying to such an old post, but it might useful for others.

    For your question number 3, you can do it with something like this

    var items = Sitecore.Context.Database.SelectItems(fastQuery);
    if (items.Length > 0)
    var comparer = Sitecore.Data.Comparers.ComparerFactory.GetComparer(items.First().Parent);
    items = items.OrderBy(i => i, comparer).ToArray();

    It’s just an example. Modify as you need.

  4. Hi Alex,

    I’ve been using this example and have created a few sorters with it. Recently I upgraded from Sitecore 6.2 to 6.4.1. I’m not sure if it is coincidental or not but I am now seeing a lot of errors when I do the compare. I am doing it with String and Int values and when I try to get the Field.Value it comes up as empty string.

    Int32 x = Convert.ToInt32(item1.Fields[“myField”].Value)

    Do you know if something has changed in Sitecore that might have caused this? When I debug through the code I can see that the field has no value. I tried calling ReadAll() before it too and this had no effect.



  5. Hi Alex,

    in case anyone else comes here with a similar issue, the answer is to wrap the code in a using block

    using (new Sitecore.SecurityModel.SecurityDisabler())

    Thanks Paul at Sitecore Support

Comments are closed.