Descending Sort

Descending Sort

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • System.Linq

Descending Sort C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/home/developers");
if (item == null) return;

Sitecore.Data.Items.Item[] children = item.Children.ToArray();
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);
output.Write("{0}: {1}", "Parent Item", item.Name);
output.RenderEndTag(); //P

var sortedList = (from entry in children orderby entry.Fields["title"].Value descending select entry); 
foreach (Sitecore.Data.Items.Item child in sortedList)
{
    output.Write("{0}", child.Fields["title"] + "<br/>");
}
output.RenderEndTag(); //Div


Descending Sort C# Web Control Output

Parent Item: Developers

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

Sources