Copied to clipboard

Flag this post as spam?

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


  • Mathias Valentin 60 posts 208 karma points
    Nov 14, 2016 @ 16:14
    Mathias Valentin
    0

    OrderBy on multiple date fields (Razor)

    I have a blog where I would like to list all blogposts in the order they were created (CreateDate), but some posts needs to be backdated, so I have created af Date picker field (datePublished) for that.

    But how do I sort/order the items with Razor. I have tried the following without success:

    .OrderBy("CreateDate, datePublished")
    Not working

    .OrderBy("CreateDate, datePublished desc")
    Not working

    .OrderBy("CreateDate desc").OrderBy("datePublished desc")
    Close but no cigar. If datePublished is filled out, the item will always appear over the other items without datePublished set.

    Maybe there is an even greater solution to this. Would be great if I was allowed to change the CreateDate field on the Properties tab. But the field is locked.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Nov 14, 2016 @ 16:34
    Alex Skrypnyk
    2

    Hi Mathias,

    Can you share code of your view or macro?

    Is it razor?

    We will find solution together.

    Thanks,

    Alex

  • Mathias Valentin 60 posts 208 karma points
    Nov 15, 2016 @ 08:32
    Mathias Valentin
    0

    This is what I have:

    var blogPosts = CurrentPage.Descendants("BlogPost").Where("Visible").OrderBy("CreateDate, metaPublishedOn desc");
    
    <ul>
    foreach (var blogPost in blogPosts){
       <li class="blogpost">...</li>
    }
    </ul>
    
  • Yasir Butt 161 posts 371 karma points
    Nov 15, 2016 @ 09:28
    Yasir Butt
    2

    I prefere to use Strong type like Model.Content instead of dynamic "CurrentPage"

    For strong type object you will get the intellisense which make your life easier. below is my code.

    var blogPosts = Model.Content.Descendants("BlogPost").Where(x=>x.GetPropertyValue<bool>("Visible")).OrderBy(x=>x.CreateDate).ThenByDescending(x=>x.GetPropertyValue<DateTime>("metaPublishedOn"));
    

    Hope it will help :)

    Yasir

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Nov 15, 2016 @ 10:02
    Alex Skrypnyk
    2

    Hi Guys,

    Yasir, thanks for suggestion, I would change it little bit:

    var blogPosts = Umbraco.AssignedContentItem.Descendants("BlogPost").Where(x => x.IsVisible()).OrderBy(x => x.CreateDate).ThenByDescending(x => x.GetPropertyValue<DateTime>("metaPublishedOn"));
    

    Thanks,

    Alex

  • Mathias Valentin 60 posts 208 karma points
    Nov 15, 2016 @ 11:04
    Mathias Valentin
    0

    Thanks for your help!! I have tried your solution Yasir, but I didn't get the result I was looking for. :/

    I think the problem is that metaPublishedOn can be empty and then the CreateDate should be used instead. If metaPublishedOn is filled out this should overrule the CreateDate. The result I got with your solution was the same thing I experienced with my own, if the metaPublishedOn was set the item would get listed first - no matter the date. Alex I don't know if your solution will help with this problem!?

    I did get the job done with the following code (feel free to comment on the code itself - still learning to code Razor properly):

    var descendants = CurrentPage.Descendants("BlogPost").Where("Visible");
    
    List<dynamic> items = new List<dynamic>();
    
    foreach (var descendant in descendants){
    
        var publishedOn = (descendant.HasValue("metaPublishedOn")) ? descendant.metaPublishedOn : descendant.CreateDate;
    
        var item = new 
        {
            publishedOn = publishedOn,
            id = descendant.Id
        };
    
        items.Add(item);
    
    }
    
    List<dynamic> sortedItems = items.OrderByDescending(x=>x.publishedOn).ToList();
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Nov 15, 2016 @ 11:29
    Alex Skrypnyk
    102

    Hi Mathias,

    You are welcome, if I understood your code right, than you can try this code:

    var blogPosts = Umbraco.AssignedContentItem.Descendants("BlogPost").Where(x => x.IsVisible()).OrderByDescending(x => x.HasValue("metaPublishedOn") ? x.GetPropertyValue<DateTime>("metaPublishedOn") : x.CreateDate);
    

    Hope it will help you.

    Be aware with dynamics and .Descendants - dangerous.

    Thanks,

    Alex

  • Mathias Valentin 60 posts 208 karma points
    Nov 15, 2016 @ 12:43
    Mathias Valentin
    0

    Spot on - Alex! That worked perfectly.

    Just curious - why and when to use AssignedContentItem over Model.Content?

  • Adrian 3 posts 71 karma points
    Nov 29, 2021 @ 05:46
    Adrian
    0

    This is a good post and pointed me in the right direction, I'll reference my Umbraco 8 solution where I had to combine two lists and sort, similar to above.

    var list1 = list; //Model.Children
    var list2 = list; //Model.Root().Children.Where(x => x.Id == INT)
    

    Combine Lists

        var combined = list1.Concat(list2).ToList();
    

    sort lists as per above example updated to umbraco 8 (i am using custom publicationDate field (metaPublishedOn used above)

    var ordered = combined.OrderByDescending(x => x.HasValue("publicationDate") ? x.Value<DateTime>("publicationDate") : x.CreateDate);
    
Please Sign in or register to post replies

Write your reply to:

Draft