Copied to clipboard

Flag this post as spam?

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


  • Roger Withnell 128 posts 613 karma points
    Jul 23, 2015 @ 12:56
    Roger Withnell
    0

    Sort pages by publish date

    I have the following code in a macro that lists information from child pages in one parent.

    @{
        foreach (var vBlogItem in Model.Content.Children)
        {
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title" style="font-size:200%">
                        @vBlogItem.GetProperty("blogTitle").Value
                    </h3>
                </div>
                <div class="panel-body">
                    <span style="font-size:120%">
                        @vBlogItem.GetProperty("blogFirstParagraph").Value
                    </span>
                </div>
            </div>
    
        }
    }
    

    Now I want to sort the children by their published date. How do I do this?

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jul 23, 2015 @ 13:11
    Anders Bjerner
    1

    Hi Roger,

    You can use LINQ to sort the items for you - eg:

    Model.Content.Children.OrderBy(x => x.UpdateDate)
    

    This will give you a collection with the newest child first (ascending order).

    If you instead want the most recent children at the top (descending order), you can change the code to:

    Model.Content.Children.OrderByDescending(x => x.UpdateDate)
    

    To update your own example, listing the children in ascending order, the code will look like:

    @{
        foreach (var vBlogItem in Model.Content.Children.OrderBy(x => x.UpdateDate))
        {
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title" style="font-size:200%">
                        @vBlogItem.GetProperty("blogTitle").Value
                    </h3>
                </div>
                <div class="panel-body">
                    <span style="font-size:120%">
                        @vBlogItem.GetProperty("blogFirstParagraph").Value
                    </span>
                </div>
            </div>
    
        }
    }
    
  • Roger Withnell 128 posts 613 karma points
    Jul 23, 2015 @ 15:01
    Roger Withnell
    0

    Thanks, Anders.

    Does .UpdateDate get the Published date?

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jul 23, 2015 @ 15:17
    Anders Bjerner
    100

    Yes, from what I can tell, the UpdateDate corresponds with the date the node was published.

    Eg. if you create a new node, fill in some data for some of the properties, and then save the node and set it to publish five minutes later, UpdateDate will reflect the timestamp for when the node was automatically published, not when you last saved the node.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 23, 2015 @ 13:14
    Dennis Aaen
    1

    Hi Roger,

    You should be able to do it by this

    @{
        foreach (var vBlogItem in Model.Content.Children.OrderBy(x => x.updateDate))
        {
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title" style="font-size:200%">
                        @vBlogItem.GetProperty("blogTitle").Value
                    </h3>
                </div>
                <div class="panel-body">
                    <span style="font-size:120%">
                        @vBlogItem.GetProperty("blogFirstParagraph").Value
                    </span>
                </div>
            </div>
    
        }
    }
    

    Hope this helps,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft