Copied to clipboard

Flag this post as spam?

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


  • Mark 122 posts 255 karma points
    Nov 05, 2014 @ 15:53
    Mark
    1

    OrderBy depending on if property exists

    Hi Guys,

    Not sure if this is possible, but I need to have an OrderBy statement on a node collection (Typed Content in this case, Descendants) that orders by a property if it exists, else order by the published date. 

    So, my chil nodes can be one of a couple of DocTypes, some have a "date" property, others do not, depending on the type.

    It'll either be something simple that I'm missing, or it really isn't possible and I'll have to do some kind of lengthy workaround..

    Any help would be appreciated though :-)

    Mark

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 05, 2014 @ 18:45
    Dennis Aaen
    1

    Hi Mark,

    If I understand your question correct you want to sort a collection on nodes, of a property if it has the property else it should sort of a other value, Maybe you would use this syntaxs to get what you want.

    @{
        <ul>
            @* OrderBy() takes the property to sort by *@
       
           
            @foreach (var page in Model.Content.Children.Where("Visible").OrderByDescending(x => x.GetPropertyValue<DateTime>("publishDate")).ThenByDescending(x => x.Name))
            {
                <li><a href="@page.Url">@page.Name</a></li>
            }
        </ul>
    }

    First I sort on a custom property called publish data, the data type is a date picker, then sort by name, and the sort order is descending. In my case I sort some news items and basic textpages,  The result of this is that the news item where the date is set, is displayed first, and then the text pages.

    Hope this helps,

    /Dennis

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Nov 08, 2014 @ 15:15
    Andy Butland
    3

    I think you can do this Mark, given you want to order by a date field in both cases - which might be what you need.  I may have misread but I think the example Dennis gives would order all your items that don't have the property either at the top or bottom of the list, which doesn't sound like what you wanted.

    So basically as above, but instead use this as the order by clause:

    .OrderBy(x => x.HasProperty("publishDate") ? x.GetPropertyValue<DateTime>("publishDate") : x.CreateDate) // or x.UpdateDate

    Will confess not tested, but looks like this should work.

    Andy

  • Mark 122 posts 255 karma points
    Nov 11, 2014 @ 17:22
    Mark
    0

    Andy,

    That is exactly what I was looking for, obvious now I look at it, especially as I'm certain I have done it before in a previous project! It does indeed work, so much appreciated. In fact, I have just put it back into an older project where I have a number of date fields to check against and chaining the query works a charm (so far...!)

    Dennis, appologies, Andy was right, that does indeed put things in groups that appear out of order, so not really what I wa after, thank you kindly for your thoughts though.

    Thanks again guys,

    Mark

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 11, 2014 @ 17:29
    Dennis Aaen
    0

    Hi Mark,

    DonĀ“t apology, I am happy that you found I solution to your case. I think that we all just try to help, as much as we can, the important thing here is that you found a solution to your problem, this time with help from Andy.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft