Copied to clipboard

Flag this post as spam?

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


  • Carlos 338 posts 472 karma points
    Jan 09, 2013 @ 00:45
    Carlos
    0

    How do you Sort By Property date/time if a property is not empty

    So I have a list of items. 
    Some of my nodes have the option to put in a date/time property that are in the list.  I want to be able to list items by ascending or descending based on the Date/Time property only IF the date/time property is filled in. 

    Here is my simple list right now.
    How do I  use the startDateTimeProperty to sort the list only IF the node has that property and if it filled in?
    Thanks in advance

     <ul id="listingPageItems">
    var startDateTime = @item.startDateTimeProperty;
    @foreach (var item in @Model.Children.Where("!hideFromAllMenus")){
    <li>
    @item.Name
     @if (!String.IsNullOrWhiteSpace(item.eventTimeHeader))
     {
    <div class="dateTimeHeader">@item.eventTimeHeader </div>
    }
    </li>
    }
    </ul>

     

     

  • Mike Chambers 636 posts 1253 karma points c-trib
    Jan 11, 2013 @ 11:47
    Mike Chambers
    0

    what are you wanting to do with those items in your enumeration that have an empty eventTimeHeader? Exclude? or place at the top or bottom of the list?

    but I think you can just add an OrderBy

     

    @Model.Children.Where("!hideFromAllMenus").OrderBy("eventTimeHeader")
    or
    @Model.Children.Where("!hideFromAllMenus").OrderByDescending("eventTimeHeader")

    if you need to get more logic in there... then you may have to revert to linq statements..

    eg OrderBy(x => (DateTime.Parse(x.HasValue("eventTimeHeader", x.GetPropertyValue("eventTimeHeader"), DateTime.Now.ToString()).ToString()))

    but that may mean further refactoring... to get your enumeration as a DynamicNodeList rather than a dynamic

Please Sign in or register to post replies

Write your reply to:

Draft