Current implementation of CreatedComparer iterates through all item versions in all languages in order to find the lowest value in the Created field.
For sites that have many items with many versions, this algorithm may be not very optimal and cause delays when expanding items in the content tree.
One of the possible solutions to improve this behavior is:
One issue with this approach is that to make it work for items, which were created before applying it, you will need to fill their "__Item Created" field manually.
This could be simply done by iterating over all items in content tree using Sitecore API and updating value of the field. You can use a sample "FillItemCreatedField.aspx" page from the step 3 below to update all content items with the lowest value of the Created field from available item versions.
Detailed description to implement this behavior is:
/sitecore/templates/System/Templates/Sections/Statistics
public class ItemEventHandler
{
public void OnItemCreated(object Sender, EventArgs args)
{
string fieldName = "__Item Created";
ItemCreatedEventArgs createdArgs = Sitecore.Events.Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;
if (createdArgs == null)
{
return;
}
Item item = createdArgs.Item;
if (item != null && item.Fields[fieldName] != null)
{
using (new EditContext(item, SecurityCheck.Disable))
{
item.Fields[fieldName].Value = Sitecore.DateUtil.ToIsoDate(DateTime.UtcNow);
}
}
}
}
<event name="item:created">
<handler type="[namespace].ItemEventHandler, [assembly name]" method="OnItemCreated"/>
</event>
public class ItemCreatedComparer : CreatedComparerFor earlier versions of Sitecore
{
protected override int DoCompare(Item item1, Item item2)
{
return this.GetItemCreationDate(item1).CompareTo(this.GetItemCreationDate(item2));
}
public override IKey ExtractKey(Item item)
{
Assert.ArgumentNotNull(item, "item");
return new KeyObj { Item = item, Key = this.GetItemCreationDate(item), Sortorder = item.Appearance.Sortorder };
}
private DateTime GetItemCreationDate(Item item)
{
if (item.Fields["__Item Created"] == null)
{
return DateTime.MinValue;
}
DateTime creationDate = Sitecore.DateUtil.ParseDateTime(item.Fields["__Item Created"].Value, DateTime.MinValue);
return creationDate;
}
}
More information on subitems sorting can be found in chapter 3.5 of the following article:
http://sdn.sitecore.net/Reference/Sitecore%206/Presentation%20Component%20API%20Cookbook.aspx
Note that the sort order has priority over the sorting rule. You can reset sort order by pressing the "Reset" button in the "Set Subitems sorting" window.