Copied to clipboard

Flag this post as spam?

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


  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Sep 03, 2009 @ 09:15
    Sebastiaan Janssen
    0

    Sorting nodes in content tree

    I have a bit of an unusual sorting need for the project I'm working on and I was wondering: is it possible to use the API to sort nodes in the content tree? 

    If not, I'll have to look at a different approach, but it would be super nice if this was possible.

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 09:22
    Thomas Höhler
    0

    You can set the SortOrder Property of the Documents by api:

    umbraco.cms.businesslogis.web.Document doc = new umbraco.cms.businesslogis.web.Document(NODEID);
    doc.sortOrder = 10;
    doc.Publish(user);
    umbraco.library.UpdateDocumentCache(doc.Id);
  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Sep 03, 2009 @ 09:23
    Sebastiaan Janssen
    0

    Cool! I'll have to loop through all the nodes myself and sort them, so no autosorting functions? 

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 09:29
    Thomas Höhler
    0

    I think so, I think the autosorting feature is only in the backend.

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 09:40
    Thomas Höhler
    1

    Implement your own comparer (in this example sorting by Expiration Date):

    using umbraco.cms.businesslogic.web;
     
    public class MyDocumentComparer: IComparer<Document>
    {
    public int Compare(Document x, Document y)
    {
    return x.ExpireDate.CompareTo(y.ExpireDate);
    }
    }

    and then compare the nodes:

    Document parentDoc = new Document(parentId);
    List<Document> childs = new List<Document>();
    childs.AddRange(parentDoc.Children);
    childs.Sort(new MyDocumentComparer());

    hth, Thomas

  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Sep 03, 2009 @ 09:52
    Sebastiaan Janssen
    0

    Sweet! Thanks for taking the time to look that up Thomas. Not sure I can use this as I have some specific needs, but I'll look into it!

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 10:31
    Thomas Höhler
    1

    Gald I coudl help.

    You can extend the Comparer with your own code. To compare some properties just compare them and return 1 for x > y, -1 for x<y and 0 for x = y

    Thomas

  • Douglas Ludlow 210 posts 366 karma points
    Apr 05, 2012 @ 22:17
    Douglas Ludlow
    0

    Thanks for your posts, guys, they really helped me out. This is how I sorted my documents:

    Document parent = new Document(sender.Parent.Id);
    List<Document> siblings = new List<Document>();
    siblings.AddRange(parent.Children);

    siblings.Sort(new MyComparer());

    for (int i = 0; i < siblings.Count; i++)
    {
    siblings[i].sortOrder = i;

    if (siblings[i].Published || siblings[i].Id == sender.Id)
    siblings[i].Publish(sender.User);
    else
    siblings[i].Save();

    library.UpdateDocumentCache(siblings[i].Id);
    }
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies