最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sort Tree Structure of Objects by Class Property - Stack Overflow

programmeradmin2浏览0评论

I have a tree structure of objects (JavaScript/TypeScript), all derived of the same class (T). Each object may or may not contain children of the same object (List). Therefore those children may or may not contain their own children (List), and so on...

I'm needing to sort each of these "levels" of nodes by a specific property of the object.

Currently I'm handling only each parent and their children:

                    nodes.sort((a, b) => {
                        return a.Id- b.Id;
                    });                       

                    nodes.forEach(function (node) {
                        node.children.sort((a, b) => {
                            return a.Id- b.Id;
                        });
                    });

However, I'm needing a method that will sort all children of children, children of those children, and etc. I imagine this will involve recursion, but I'm not sure the best way of handling this in terms of efficiency.

I have a tree structure of objects (JavaScript/TypeScript), all derived of the same class (T). Each object may or may not contain children of the same object (List). Therefore those children may or may not contain their own children (List), and so on...

I'm needing to sort each of these "levels" of nodes by a specific property of the object.

Currently I'm handling only each parent and their children:

                    nodes.sort((a, b) => {
                        return a.Id- b.Id;
                    });                       

                    nodes.forEach(function (node) {
                        node.children.sort((a, b) => {
                            return a.Id- b.Id;
                        });
                    });

However, I'm needing a method that will sort all children of children, children of those children, and etc. I imagine this will involve recursion, but I'm not sure the best way of handling this in terms of efficiency.

Share edited Jul 23, 2015 at 21:19 cdalto asked Jul 23, 2015 at 21:10 cdaltocdalto 8573 gold badges16 silver badges33 bronze badges 6
  • 1 There's no efficiency involved here. You need to sort N independant lists. That's O(N). Use recursion. – Amit Commented Jul 23, 2015 at 21:13
  • @Amit O(N) for each list you say! – Fals Commented Jul 23, 2015 at 21:15
  • 2 @Fals I meant O(N) where N is the number of lists ("children"). The sorting itself is not shown so I can't assume anything about the type of sorting used, but obviously every list has to be sorted as well. – Amit Commented Jul 23, 2015 at 21:17
  • If there is not a more efficient way, then my biggest issue with understanding this is syntax. – cdalto Commented Jul 23, 2015 at 21:20
  • As you already know you will need recursion for this, have you tried implementing it? Please show us your approach. – Bergi Commented Jul 23, 2015 at 21:24
 |  Show 1 more ment

1 Answer 1

Reset to default 6

a) Sort the nodes.

b) Go through each node, if the node has children, repeat step A with its children.

function sortNodesAndChildren(nodes) {
    nodes.sort();
    nodes.forEach(function (node) {
        if (nodeHasChildren(node)) {
            sortNodesAndChildren(node.children);
        }
    })
}
发布评论

评论列表(0)

  1. 暂无评论