Copied to clipboard

Flag this post as spam?

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


  • Jedd France 29 posts 140 karma points
    Jul 30, 2016 @ 07:33
    Jedd France
    1

    Listing Articles in Date Order

    Hello everyone,

    I have a macro that pulls out some info from all pages with doctype 'article'

    The code is as follows:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{  var home = CurrentPage.Site();
      var postNodes = home.Descendants("article");
    
    }
    
    @foreach (var item in postNodes)
    {
        <div class="article">
            <p class="tag">@item.tag</p>
            <a href="@item.url"><img class="thumbnail" src="@item.thumbnail"/></a>
            <a href="@item.url"><h1>@item.title.ToUpper()</h1></a>
            @Html.Raw(item.snippet)
            <a class="continueLink" href="@item.url">[ Continue reading.. ] </a>
        </div>
    }
    

    The only problem I am having, is the output is listed in order of tree position, however I want them to be showed in date order (newest first).

    Is there a quick fix?

    Thanks in advance!

    -Jedd

  • Yasir Butt 161 posts 371 karma points
    Jul 30, 2016 @ 09:24
    Yasir Butt
    1

    postNodes.orderBy(x=>x.GetPropertyValue

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 30, 2016 @ 11:06
    Alex Skrypnyk
    100

    Hi Jedd,

    You have to add ordering as said Yasir, I would suggest for you to use createdDate field which is general for Umbraco nodes, like that:

    var postNodes = home.Descendants("article").OrderBy("createDate desc").ToList();
    

    Thanks,

    Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2016 @ 12:49
    Alex Skrypnyk
    1

    Hi Jedd,

    Also be aware that dynamic objects has this strange ways for sorting - OrderBy("createDate desc"). Much better will be to use strongly typed models, sorting will be much easier to understand and to maintain.

    OrderByDescending(x => x.CreatedDate)
    

    But you need to rewrite all view.

    Thanks,

    Alex

  • Jedd France 29 posts 140 karma points
    Jul 31, 2016 @ 13:29
    Jedd France
    0

    Hi Alex,

    That works perfectly! Thank you for helping me out yet again!

    Have a great day,

    Jedd

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2016 @ 13:39
    Alex Skrypnyk
    0

    You are welcome, Jedd.

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft