Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    May 30, 2011 @ 23:12
    Simon Dingley
    0

    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:

    Model.AncestorOrSelf(1).Descendants("NewsItem").Take(numItemsToDisplay)

    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:

    Model.AncestorOrSelf(1).Descendants("NewsItem").OrderBy("createdDate").Take(numItems

    ...but it seems there is no OrderByDescending method and so I wondered what the best approach is to address the issue with Razor?

  • Trevor Loader 199 posts 256 karma points
    May 30, 2011 @ 23:43
    Trevor Loader
    0

    I read somewhere that the syntax is

    Model.AncestorOrSelf(1).Descendants("NewsItem").OrderBy("createdDate desc").Take(numItems

    But I cannot seem to get that to work either!  Looking forward to someone figuring this out.

     

  • Richard Boelen 61 posts 153 karma points
    May 31, 2011 @ 09:08
    Richard Boelen
    1

    You could try this:

    @{
        umbraco.MacroEngines.DynamicNodeList list = Model.AncestorOrSelf(1).Descendants("NewsItem");
        var orderedlist = list.Items.OrderByDescending(=> x.CreateDate).Take(numItemsToDisplay);
        foreach(dynamic item in orderedlist){
            <ul>
                <li>@item.Name li>
            <ul>
        }
    }

    Cheers, Richard

  • James Drever 118 posts 149 karma points
    May 31, 2011 @ 09:18
    James Drever
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft