Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jqtree-排序入口_Javascript_Sorting_Jqtree - Fatal编程技术网

Javascript Jqtree-排序入口

Javascript Jqtree-排序入口,javascript,sorting,jqtree,Javascript,Sorting,Jqtree,我正在使用jqtree插件生成一个树视图。 现在我想通过标签对每个节点的树进行排序 这个插件没有排序功能,所以我想我需要在加载数据之前对它进行排序 这是我要整理的数据 [ { "label": "A label", "id": "201", "children": [ { "label": "C Label", "id": "40773", "children": [

我正在使用jqtree插件生成一个树视图。 现在我想通过标签对每个节点的树进行排序

这个插件没有排序功能,所以我想我需要在加载数据之前对它进行排序

这是我要整理的数据

[
{
    "label": "A label",
    "id": "201",
    "children": [
        {
            "label": "C Label",
            "id": "40773",
            "children": [
                {
                    "label": "F label",
                    "id": "222460",
                    "children": []
                },
                {
                    "label": "C label",
                    "id": "222469",
                    "children": []
                },
                {
                    "label": "X label",
                    "id": "27512",
                    "children": [
                        {
                            "label": "F label",
                            "id": "143546",
                            "children": []
                        },

                        {
                            "label": "D label",
                            "id": "141341",
                            "children": [
                                {
                                    "label": "G label",
                                    "id": "222456",
                                    "children": []
                                },
                                {
                                    "label": "L label",
                                    "id": "222457",
                                    "children": []
                                },
                                {
                                    "label": "x label",
                                    "id": "222443",
                                    "children": [
                                        {
                                            "label": "Z label",
                                            "id": "222447",
                                            "children": []
                                        },
                                        {
                                            "label": "A label",
                                            "id": "222446",
                                            "children": []
                                        }
                                    ]
                                },
                                {
                                    "label": "L label",
                                    "id": "222455",
                                    "children": []
                                }
                            ]
                        },
                        {
                            "label": "A label",
                            "id": "143547",
                            "children": [
                                {
                                    "label": "B label",
                                    "id": "222458",
                                    "children": []
                                }
                            ]
                        },
                        {
                            "label": "R label",
                            "id": "143548",
                            "children": []
                        }
                    ]
                }
            ]
        }
    ]
}
]


非常感谢您的建议。

您可以使用递归函数对树进行迭代,并使用任何排序算法对它们进行排序

recursiveSortByLabel(arr); //arr = the array with your tree data
function recursiveSortByLabel(obj){
    if(obj.length > 0){
        for(var i in obj){
            if(obj[i].children.length > 0)
                recursiveSortByLabel(obj[i].children);
            sortAlgorithm(obj); //any sorting algorithm
        }
    }
}
在本文中,我使用了效率不高但易于实现的bubblesort算法。它将结果记录到控制台进行检查