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 89 posts 290 karma points
    5 days ago
    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 1083 posts 4456 karma points MVP 2x c-trib
    5 days ago
    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 89 posts 290 karma points
    1 day ago
    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