我不知道该怎么解释,但这是我想要的。我有一个也包含一个健康值的域数组,这就是我想要对它进行排序的方法。如果健康值未知,则当前healthKnown设置为false,健康设置为95。
aaa.com - 100
bbb.com - 100
ccc.com - 100
aaa.com - 90
bbb.com - 90
ccc.com - 90
aaa.com - (unknown)
bbb.com - (unknown)
ccc.com - (unknown)(但不会有任何副本)
在这种情况下,具有相同健康状态的每一组域都是按字母顺序排序的,未知健康度是最后一组。数组看起来是这样的。
[
{
"name": "example1.com",
"details": ...,
"health": 100,
"healthKnown: true
},
{
"name": "example2.com",
"details": ...,
"health": 100,
"healthKnown: true
}
]发布于 2021-02-26 09:47:03
根据注释,array.sort()可能是最好的方法。见示例
let domains = [
{
"name": "a.example1.com",
"details": "",
"health": 100,
"healthKnown": true
},
{
"name": "c.example2.com",
"details": "",
"health": 100,
"healthKnown": true
},
{
"name": "b.example2.com",
"details": "",
"health": 100,
"healthKnown": true
}
]
domains
.sort((a,b) => b.healthKnown && a.health > b.health ? 1 : -1)
// credits to https://stackoverflow.com/a/61033232/833499
.sort((a, b) => a.name.localeCompare(b.name))
console.log(domains);https://stackoverflow.com/questions/66382963
复制相似问题