Copied to clipboard

Flag this post as spam?

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


  • Christian Tarrild 23 posts 43 karma points
    May 14, 2010 @ 16:40
    Christian Tarrild
    0

    Sorting of members in Custom tree

    Hi I am writing an backend extension that lists all members. I have already done that, however I would like the list to be sorted by name of the member. I have tried using tree.treeCollection.Sort() with the code below, but by doing that no members is shown in the tree. How do I sort the members in the code below?

    public override void Render(ref XmlTree tree)
            {
                var members = Member.GetAll;
    
                foreach (Member m in members)
                {
                XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode.Text = m.getProperty("Navn").Value.ToString();
                xNode.Action = "javascript:openMember('" + m.Id +"')";
                xNode.Icon = "member.gif";
                xNode.OpenIcon = "member.gif";
                tree.Add(xNode);
                }
            }

    Regard Tarrild

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 14, 2010 @ 23:16
    Aaron Powell
    1

    If you're using .NET 3.5 you can just run LINQ over the top of it:

    public override void Render(ref XmlTree tree)
           
    {
               
    var members = Member.GetAll;
           
               
    foreach (Member m in members.OrderBy(x => x.Text))
               
    {
               
    XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode
    .Text = m.getProperty("Navn").Value.ToString();
                xNode
    .Action = "javascript:openMember('" + m.Id +"')";
                xNode
    .Icon = "member.gif";
                xNode
    .OpenIcon = "member.gif";
                tree
    .Add(xNode);
               
    }
           
    }

  • Christian Tarrild 23 posts 43 karma points
    May 15, 2010 @ 18:53
    Christian Tarrild
    0

    I have never used LINQ before. Could you provide me with a short example?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 16, 2010 @ 13:16
    Aaron Powell
    0

    Err, that was the example.

    The statement:

    .OrderBy(x => x.Text)

    Is a call to an extension method (http://msdn.microsoft.com/en-us/library/bb534966.aspx) which takes a delegate which returns the key to sort on. x => x.Text is a lambda function, where x is an instance of Member, defined by generics.

  • Christian Tarrild 23 posts 43 karma points
    May 21, 2010 @ 12:14
    Christian Tarrild
    0

    Sorry, I was not observant enough to see the differences in the code. 

    Thank you very much for your reply, it worked like a charm.

Please Sign in or register to post replies

Write your reply to:

Draft