Copied to clipboard

Flag this post as spam?

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


  • Mark Watson 104 posts 345 karma points
    Aug 15, 2019 @ 03:55
    Mark Watson
    0

    Date picker sort order not working

    I have tried to get this to work several ways but it just seems not to work.

    @{
    var selection = Umbraco.Content(Guid.Parse("4cc0a5b1-010f-406f-9d72-2689074b8142"))
    .Children()
    .Where(x => x.IsVisible())
    .OrderByDescending(x => x.HasValue("newsPublishedDate"));
    

    } @foreach (var item in selection) {

    @item.Name

    @item.Value("newsBodyText")

    </div>
    

    }

    Previously I have also used

        @foreach(var item in selection.OrderBy("newsPublishedDate")){
    

    But neither sorts the items by the date picker "newsPublishedDate"

    Does any one have any ideas?

  • Nik 1155 posts 4743 karma points MVP 2x c-trib
    Aug 15, 2019 @ 09:27
    Nik
    100

    Hi Mark,

    I think your first option is the closest but your OrderBy Descending is an issue.

    .OrderByDescending(x => x.HasValue("newsPublishedDate")
    

    HasValue returns true or false, so assuming they all have a date then its sorting on "true" for everything.

    What you probably want is:

    .OrderByDescending(x => x.Value<DateTime>("newsPublishedDate")
    

    Something to consider, what do you want it to do when multiple items have the same newsPublishedDate ?

    Maybe something like this?

     .OrderByDescending(x => x.Value<DateTime>("newsPublishedDate").ThenBy(x=>x.SortOrder)  
    

    Disclaimer: I can't recall the exact syntax for the ThenBy bit but I think that is right.

    Thanks

    Nik

  • Mark Watson 104 posts 345 karma points
    Aug 19, 2019 @ 02:49
    Mark Watson
    0

    Thanks Nik. Worked perfectly Yes the syntax is correct

Please Sign in or register to post replies

Write your reply to:

Draft