Now this works fine however I want the descendant nodes in descending order so that I can display the latest x items. I can return them in ascending order as follows:
The syntax definitely is "createdDate desc" but I've found sorting in Razor is inconsistently applied. I've had sorting working ok with strings, but haven't tried dates.
Ordering Descendant Nodes
Still getting to grips with Razor and have hit a stumbling block. I have a collection of nodes I get as follows:
Now this works fine however I want the descendant nodes in descending order so that I can display the latest x items. I can return them in ascending order as follows:
...but it seems there is no OrderByDescending method and so I wondered what the best approach is to address the issue with Razor?
I read somewhere that the syntax is
But I cannot seem to get that to work either! Looking forward to someone figuring this out.
You could try this:
@{
umbraco.MacroEngines.DynamicNodeList list = Model.AncestorOrSelf(1).Descendants("NewsItem");
var orderedlist = list.Items.OrderByDescending(x => x.CreateDate).Take(numItemsToDisplay);
foreach(dynamic item in orderedlist){
<ul>
<li>@item.Name li>
<ul>
}
}
Cheers, Richard
The syntax definitely is "createdDate desc" but I've found sorting in Razor is inconsistently applied. I've had sorting working ok with strings, but haven't tried dates.
The sorting issues have been previously reported. Looks like these issues are/will be resolved in 4.7.1 - see http://our.umbraco.org/forum/developers/razor/19400-OrderBy-not-working?p=0
is working on a reply...