Copied to clipboard

Flag this post as spam?

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


  • David 20 posts 131 karma points
    Dec 17, 2018 @ 23:51
    David
    0

    Sort by news 'sticky' and by 'Date Desc' expression

    Newbie Alert :D

    I have a checkbox in my 'News Article' page with an Alias of 'sticky'. How can I sort by this being checked, and by date descending, So sticky posts are set the top of the list, ordered by date?

    The below does sort by Date but does not take into account if a post has the 'sticky' checkbox set to true. Do I need to do something else in my expression?

    var newsChildPages = Model.Content.Children.Where(x => x.DocumentTypeAlias == "newsArticle").OrderBy(a => a.GetPropertyValue<bool>("sticky")).ThenByDescending(x => x.GetPropertyValue<DateTime>("newsArticleDate"));
    

    Thanks

    -David

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Dec 18, 2018 @ 00:42
    Alex Skrypnyk
    100

    Hi David

    Try this code:

    var newsChildPages = Model.Content.Children.Where(x => x.DocumentTypeAlias == "newsArticle")
            .OrderBy(a => a.GetPropertyValue<bool>("sticky") ? 1 : 0)
            .ThenByDescending(x => x.GetPropertyValue<DateTime>("newsArticleDate"));
    

    This line should do the trick:

    .OrderBy(a => a.GetPropertyValue<bool>("sticky") ? 1 : 0)
    
  • David 20 posts 131 karma points
    Dec 18, 2018 @ 21:18
    David
    0

    Thanks so much, Alex :-)

Please Sign in or register to post replies

Write your reply to:

Draft