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
    Jun 09, 2016 @ 11:04
    Roger Withnell
    1

    Sorting children

    My News item node has child nodes like so:

    News item Item 1 Item 2 Item 3

    When rendered on the page, I want to display in the reverse order: Item 3 Item 2 Item 1

    When a new item is added, it'll be at the bottom of the list in the back office and top of the list on the page. The order can be changed by sorting the items in the back office.

    I've tried:

    foreach (var vMessageItem in vFolderNode.Children.OrderBy("sortOrder desc"))
    

    which doesn't work.

    What is the correct code, please?

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Jun 09, 2016 @ 11:11
    Dennis Adolfi
    101

    Hi Roger.

    Try this:

    foreach (var vMessageItem in vFolderNode.Children.Reverse())
    
  • MarcC 49 posts 356 karma points
    Jun 09, 2016 @ 12:58
    MarcC
    1

    Dennis is spot on, in order to fix the code you supplied you will need to add in a 'key' that you wish to sort them by, in this case we use the node Name. You might wish to use publish date.

    foreach (var vMessageItem in vFolderNode.Children.OrderBy("sortOrder desc"))
    

    Would become:

    foreach (var vMessageItem in vFolderNode.Children.OrderBy(x => x.Name))
    

    This just sorts the method parameters you can use OrderByDesc with this to achieve what Dennis has said.

    foreach (var vMessageItem in vFolderNode.Children.OrderByDesc(x => x.Name))
    
  • Sotiris Filippidis 286 posts 1501 karma points
    Jun 12, 2016 @ 20:15
    Sotiris Filippidis
    0

    Isn't it OrderByDescending instead of OrderByDesc?

    like:

    foreach (var vMessageItem in vFolderNode.Children.OrderByDescending(x => x.Name))
    
  • Eli 15 posts 95 karma points
    Jan 30, 2019 @ 01:04
    Eli
    0

    Hi all,

    What if I do not want to sort all of the children (can be a huge number) every time? What if I want that on every child page creation, the pages will be sorted automatically, so when I query for the children I'll get a sorted list?

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft