Wednesday, 05 August 2009
Sorting your items on your website
Alex de Groot wrote an excellent article the other day where he explains about one of the often forgot, but powerful features in Sitecore: Sorting Sitecore Items: http://sitecore.alexiasoft.nl/2009/08/04/sorting-sitecore...
His article even illustrated how to write a comparer and use it in Sitecore.
Essentially, Alex’s article explains how to assign a sorter:
You select a node in the content structure, and then select a sorting mechanism which will applied to existing child items as well as to new child items (provided that you haven’t been sorting manually, then this takes precedence).
Whether you publish, - or run live mode on your web site, any time you request a child list of a node, the order you see in your content structure will also be the order you get the list in. So, if your content in the content structure is sorted by item name, your item.Children will return this order as well.
But what happens if you want to create a web control which sorts the content in another order, for example by creation date?
Why not reuse the comparers you have in Sitecore for this purpose?
Sitecore.Data.Items.Item contextItem = this.GetItem();
ChildList children = new ChildList(contextItem);
children.Sort(new Sitecore.Data.Comparers.CreatedComparer());
Pretty neat, right?
But why not take it one step further and create a web control where whoever inserts the web control may choose the comparer at that time: Instead of coding into your web control which sort mechanism to use, why not a take this value from a web control property?
And while at it, - why not built a rendering properties template that allows the editor to select the sort mechanism from a dropdown list:

Create custom property
To build the UI with the “sort” dropdown, first build a template inheriting from “Standard Rendering Parameters” (/system/layout/rendering parameters/standard rendering parameters.
Add a single field, “Comparer” (must match the property in your web control), with the data type “Droplink”. Also set the data source to the comparers node: /sitecore/system/settings/subitems sorting.

Create web control
Next, build or extend your web control to accept a Comparer property. This property will accept a string with the GUID referring to the item where the comparer is defined. The code for this control looks like this:
namespace UsingComparers {
public class SimpleMenu: Sitecore.Web.UI.WebControl {
private Sitecore.Data.Comparers.Comparer _comparer =
new Sitecore.Data.Comparers.DefaultComparer();
private string _comparerIDString =
"{781247D2-9785-400F-8935-C818EC757967}"; // Default comparer
public string Comparer {
get {
return _comparerIDString;
}
set {
_comparerIDString = value;
Sitecore.Data.ID comparerID =
Sitecore.Data.ID.Parse(_comparerIDString);
Sitecore.Data.Items.Item ComparerItem =
Sitecore.Context.Database.GetItem(comparerID);
string comparerClassField = ComparerItem["Type"];
_comparer =
Sitecore.Reflection.ReflectionUtil.CreateObject(
comparerClassField, new object[0])
as Sitecore.Data.Comparers.Comparer;
}
}
protected override void DoRender(HtmlTextWriter output) {
Sitecore.Data.Items.Item contextItem = this.GetItem();
ChildList children = new ChildList(contextItem);
children.Sort(_comparer);
foreach(Sitecore.Data.Items.Item child in children){
output.Write(child.Name + "<br />");
}
}
}
}
Register web control
Register your web control as a new web control rendering (/sitecore/layout/renderings), and select the “Parameters Template” field to the template defined above:

14:52 Posted in Sitecore | Permalink | Comments (0) | Email this | Tags: sitecore, sorting, comparer


