Tuesday, December 12, 2006

Understanding the Sort Order


1) In Sitecore 5.3 this code returns the home children in the same way as in Shell UI:

Item root = db.Items["/sitecore/content/home"];

ChildList list = root.GetChildren(ChildListOptions.IgnoreSecurity);

foreach (Item itm in list) { Response.Write(itm.Name); }

2) Sitecore sorts children according to their SortOrder field firstly.

3) If items do not have the SortOrder field filled or the values are equal Sitecore sorts it according to “Subitems Sorting” field value of the parent item.

4) You can apply your own sorting by writing your own IComparer. Please add your own
/sitecore/system/Settings/Subitems Sorting/* comparer with such code structure:

public class CreatedComparer : Comparer

{

protected override int DoCompare(Item item1, Item item2)

{

// return item1.Statistics.Created.CompareTo

// (item2.Statistics.Created);

return {0 or 1 or -1};

}

}

5) Note: you can apply this to all the items based on a certain template by setting it via “Standard Values”.

3 comments:

Anonymous said...

Hi Alex,

Really nice posts on sorting and image uploads!

Kim Hornung said...

Hi Alex,

Nice article! Not very many people seem to know about the dynamick "subitems sorting" option in v5.3.

But I wonder about one thing...

You write: "If items do not have the SortOrder field filled or the values are equal Sitecore sorts it according to 'Subitems Sorting' field value of the parent item".

I would have thought that it was the other way round: that Sitecore sorts by 'Subitems Sorting' if it is specified - and if not, then by SortOrder (and item name as second sorting key).

/Kim

Esben said...

I know it's an old post, but you write that "In Sitecore 5.3 this code returns the home children in the same way as in Shell UI", and that's exactly what I am looking for: to retrieve the children of an item in the same order as the one they have in the content editor. I am not able to achieve that, though.

I have an item with no subitem sorting set. The item has two children C1 and C2. C1 is the first element in the content editor - it has sortorder=200 and C2 has sortorder=300 (I moved C1 to the top manually). If I use

ChildList children = myItem.GetChildren(ChildListOptions.IgnoreSecurity);

to retrieve the children, C2 comes out on top! :( Any ideas?