与父母关系按字母顺序分类一棵树

发布于 2025-01-19 02:31:32 字数 3769 浏览 0 评论 0原文

我正在尝试创建一个编码环境,它有一个文件系统,用户可以在其中添加或删除文件,该文件系统是一个树结构,当用户更新该文件系统上的任何内容时,我试图按字母顺序对树进行排序。我尝试在互联网上查找但找不到太多。

[
    {
        "name": "src",
        "id": "DfGfg_ckUK_YRM0Guwz-U",
        "type": "dir",
        "path": "/src",
        "children": [
            {
                "name": "App.css",
                "id": "VZ0DztZohJ5QNZA_MJEJj",
                "type": "file",
                "path": "/src/App.css",
            },
            {
                "name": "style.css",
                "id": "6djglLnepQ5VKpE02FfbJ",
                "type": "file",
                "path": "/src/style.css",
            },
            {
                "name": "components",
                "id": "LQtSXX8bXPRskbQGjBIsU",
                "type": "dir",
                "path": "/src/components",
                "children": [
                    {
                        "name": "Clicker.js",
                        "id": "B-qo2rzjElEq_LdbflIik",
                        "type": "file",
                        "path": "/src/components/Clicker.js",
                    }
                ]
            },
            {
                "name": "index.js",
                "id": "A3aR70uZ-OA1dwLNon7RA",
                "type": "file",
                "path": "/src/index.js",
            },
            {
                "name": "App.js",
                "id": "_43kjHrcpFAvlvn0orNe6",
                "type": "file",
                "path": "/src/App.js",
            }
        ]
    },
    {
        "name": "public",
        "id": "CaQSW19bFlIzdd5l0xKXX",
        "type": "dir",
        "path": "/public",
        "children": [
            {
                "name": "index.html",
                "id": "B1QcK9n8eRVVNF0N_jTeR",
                "type": "file",
                "path": "/public/index.html",
            }
        ]
    }
]

排序后的结构应如下所示。 type='dir' 首先出现(按字母顺序),然​​后是它的文件,这些文件也已排序。任何帮助将不胜感激。

[
    {
        "name": "public",
        "id": "CaQSW19bFlIzdd5l0xKXX",
        "type": "dir",
        "path": "/public",
        "children": [
            {
                "name": "index.html",
                "id": "B1QcK9n8eRVVNF0N_jTeR",
                "type": "file",
                "path": "/public/index.html",
            }
        ]
    },
    {
        "name": "src",
        "id": "DfGfg_ckUK_YRM0Guwz-U",
        "type": "dir",
        "path": "/src",
        "children": [
            {
                "name": "components",
                "id": "LQtSXX8bXPRskbQGjBIsU",
                "type": "dir",
                "path": "/src/components",
                "children": [
                    {
                        "name": "Clicker.js",
                        "id": "B-qo2rzjElEq_LdbflIik",
                        "type": "file",
                        "path": "/src/components/Clicker.js",
                    }
                ]
            },
            {
                "name": "App.css",
                "id": "VZ0DztZohJ5QNZA_MJEJj",
                "type": "file",
                "path": "/src/App.css",
            },
            {
                "name": "App.js",
                "id": "_43kjHrcpFAvlvn0orNe6",
                "type": "file",
                "path": "/src/App.js",
            },
            {
                "name": "index.js",
                "id": "A3aR70uZ-OA1dwLNon7RA",
                "type": "file",
                "path": "/src/index.js",
            },
            {
                "name": "style.css",
                "id": "6djglLnepQ5VKpE02FfbJ",
                "type": "file",
                "path": "/src/style.css",
            },
        ]
    },
]

谢谢。

I am trying to make a coding environment it has a file system in which users could add or delete files, the file system is a tree structure and I am trying to sort the tree alphabetically when users update anything on that filesystem. I have tried looking on the internet but couldn't find much.

[
    {
        "name": "src",
        "id": "DfGfg_ckUK_YRM0Guwz-U",
        "type": "dir",
        "path": "/src",
        "children": [
            {
                "name": "App.css",
                "id": "VZ0DztZohJ5QNZA_MJEJj",
                "type": "file",
                "path": "/src/App.css",
            },
            {
                "name": "style.css",
                "id": "6djglLnepQ5VKpE02FfbJ",
                "type": "file",
                "path": "/src/style.css",
            },
            {
                "name": "components",
                "id": "LQtSXX8bXPRskbQGjBIsU",
                "type": "dir",
                "path": "/src/components",
                "children": [
                    {
                        "name": "Clicker.js",
                        "id": "B-qo2rzjElEq_LdbflIik",
                        "type": "file",
                        "path": "/src/components/Clicker.js",
                    }
                ]
            },
            {
                "name": "index.js",
                "id": "A3aR70uZ-OA1dwLNon7RA",
                "type": "file",
                "path": "/src/index.js",
            },
            {
                "name": "App.js",
                "id": "_43kjHrcpFAvlvn0orNe6",
                "type": "file",
                "path": "/src/App.js",
            }
        ]
    },
    {
        "name": "public",
        "id": "CaQSW19bFlIzdd5l0xKXX",
        "type": "dir",
        "path": "/public",
        "children": [
            {
                "name": "index.html",
                "id": "B1QcK9n8eRVVNF0N_jTeR",
                "type": "file",
                "path": "/public/index.html",
            }
        ]
    }
]

The sorted structure should come out like the below.
The type='dir' comes first (alphabetically) and then its files which are also sorted. Any help would be appreciated.

[
    {
        "name": "public",
        "id": "CaQSW19bFlIzdd5l0xKXX",
        "type": "dir",
        "path": "/public",
        "children": [
            {
                "name": "index.html",
                "id": "B1QcK9n8eRVVNF0N_jTeR",
                "type": "file",
                "path": "/public/index.html",
            }
        ]
    },
    {
        "name": "src",
        "id": "DfGfg_ckUK_YRM0Guwz-U",
        "type": "dir",
        "path": "/src",
        "children": [
            {
                "name": "components",
                "id": "LQtSXX8bXPRskbQGjBIsU",
                "type": "dir",
                "path": "/src/components",
                "children": [
                    {
                        "name": "Clicker.js",
                        "id": "B-qo2rzjElEq_LdbflIik",
                        "type": "file",
                        "path": "/src/components/Clicker.js",
                    }
                ]
            },
            {
                "name": "App.css",
                "id": "VZ0DztZohJ5QNZA_MJEJj",
                "type": "file",
                "path": "/src/App.css",
            },
            {
                "name": "App.js",
                "id": "_43kjHrcpFAvlvn0orNe6",
                "type": "file",
                "path": "/src/App.js",
            },
            {
                "name": "index.js",
                "id": "A3aR70uZ-OA1dwLNon7RA",
                "type": "file",
                "path": "/src/index.js",
            },
            {
                "name": "style.css",
                "id": "6djglLnepQ5VKpE02FfbJ",
                "type": "file",
                "path": "/src/style.css",
            },
        ]
    },
]

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您将需要:

  • 按所示的顶级节点(按类型,然后按名称)
  • 对每个节点的子女进行相同的操作,

这是这样的外观:

function sortHierarchy(nodes) {
    nodes.sort((a, b) => a.type.localeCompare(b.type) || a.name.localeCompare(b.name));
    nodes.forEach(({children}) => sortHierarchy(children ?? []));
}

// Demo
const hierarchy = [{"name": "src","id": "DfGfg_ckUK_YRM0Guwz-U","type": "dir","path": "/src","children": [{"name": "App.css","id": "VZ0DztZohJ5QNZA_MJEJj","type": "file","path": "/src/App.css",},{"name": "style.css","id": "6djglLnepQ5VKpE02FfbJ","type": "file","path": "/src/style.css",},{"name": "components","id": "LQtSXX8bXPRskbQGjBIsU","type": "dir","path": "/src/components","children": [{"name": "Clicker.js","id": "B-qo2rzjElEq_LdbflIik","type": "file","path": "/src/components/Clicker.js",}]},{"name": "index.js","id": "A3aR70uZ-OA1dwLNon7RA","type": "file","path": "/src/index.js",},{"name": "App.js","id": "_43kjHrcpFAvlvn0orNe6","type": "file","path": "/src/App.js",}]},{"name": "public","id": "CaQSW19bFlIzdd5l0xKXX","type": "dir","path": "/public","children": [{"name": "index.html","id": "B1QcK9n8eRVVNF0N_jTeR","type": "file","path": "/public/index.html",}]}];

sortHierarchy(hierarchy);
console.log(hierarchy);

You would need to:

  • sort the top level nodes as indicated (by type, then by name)
  • perform the same on the children of each node in that level

Here is how that looks:

function sortHierarchy(nodes) {
    nodes.sort((a, b) => a.type.localeCompare(b.type) || a.name.localeCompare(b.name));
    nodes.forEach(({children}) => sortHierarchy(children ?? []));
}

// Demo
const hierarchy = [{"name": "src","id": "DfGfg_ckUK_YRM0Guwz-U","type": "dir","path": "/src","children": [{"name": "App.css","id": "VZ0DztZohJ5QNZA_MJEJj","type": "file","path": "/src/App.css",},{"name": "style.css","id": "6djglLnepQ5VKpE02FfbJ","type": "file","path": "/src/style.css",},{"name": "components","id": "LQtSXX8bXPRskbQGjBIsU","type": "dir","path": "/src/components","children": [{"name": "Clicker.js","id": "B-qo2rzjElEq_LdbflIik","type": "file","path": "/src/components/Clicker.js",}]},{"name": "index.js","id": "A3aR70uZ-OA1dwLNon7RA","type": "file","path": "/src/index.js",},{"name": "App.js","id": "_43kjHrcpFAvlvn0orNe6","type": "file","path": "/src/App.js",}]},{"name": "public","id": "CaQSW19bFlIzdd5l0xKXX","type": "dir","path": "/public","children": [{"name": "index.html","id": "B1QcK9n8eRVVNF0N_jTeR","type": "file","path": "/public/index.html",}]}];

sortHierarchy(hierarchy);
console.log(hierarchy);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文