本文主要是介绍JavaScript字符串排序(按名称排序),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
let arr = [
{
name: "移动",
id: 1,
children: [
{
name: "联通-004",
id: 4,
children: [{
name: "电信-007",
id: 7
},
{
name: "无线网-008",
id: 8
},
{
name: "电信-008",
id: 9
}]
},
{
name: "123-005",
id: 5
},
{
name: "联通-006",
id: 6
}]
},
{
name: "001通讯",
id: 2
},
{
name: "003通讯",
id: 3
},
{
name: "002通讯",
id: 10
}
]
function recursion(data) {
data.map(e => {
if (e.children) {
e.children.sort(function (a, b) { return a.name.localeCompare(b.name, 'zh-CN') })
recursion(e.children)
}
})
return data
}
arr.sort(function (a, b) { return a.name.localeCompare(b.name, 'zh-CN') })
arr = recursion(arr)
console.log(arr);
</script>
</html>
这篇关于JavaScript字符串排序(按名称排序)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!