Sort Child Items

Sort Child Items

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

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

Sort Child Items C# Web Control Code

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

Sitecore.Collections.ChildList children = item.GetChildren();
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

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

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


Sort Child Items XSLT Rendering Code

<xsl:variable name="item" select="./../../.." />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">
<!-- using &#58; for colon and &#160; for space -->
<p><b><xsl:value-of select="'Parent Item'"/>
&#58;&#160;<sc:text field="title" select="$item"/></b></p>

<xsl:for-each select="$item/item">
   <xsl:sort select="sc:fld('title',.)" data-type="text" order="ascending"/>
</xsl:for-each>

<xsl:for-each select="$item/item">
   <b><sc:text field="title" select="." disable-web-editing="true"/></b><br/>
</xsl:for-each>

</div>


Sort Child Items 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

Sort Child Items XSLT Rendering Output

Parent Item : Sitecore Developers

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