ChildList Sort

ChildList Sort

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.ChildList
  • Sitecore.Data.Comparers.ItemComparer
  • Sitecore.Data.Items.Item

ChildList Sort C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem().Parent.Parent.Parent;
if (item == null) return;

Sitecore.Collections.ChildList children = new Sitecore.Collections.ChildList(item);
if (children == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Parent Item", item.Name);
output.RenderEndTag(); //P

Sitecore.Data.Comparers.ItemComparer compare = new Sitecore.Data.Comparers.ItemComparer();
children.Sort(compare);

int i = 0;
foreach (Sitecore.Data.Items.Item child in children)
{
    i++;
    if(i < children.Count)
        output.Write("{0}", "<b>" + child.DisplayName + "</b><br/>");
    else
        output.Write("{0}", "<b>" + child.DisplayName + "</b>");
    
}
output.RenderEndTag(); //Div


ChildList Sort C# Web Control Output

Parent Item: Developers

ASP.NET
Caching
Content Display
Content Promotion
Custom Interfaces
Databases
Data Templates
Devices
Field Types
Media
Modules
Presentation
Scripting Languages
Security
Sitecore Sites
Sitecore Tools