嗨,我在做游戏应用程序。游戏数据库的架构如下
[
{
UserName,
UserImage,
UserScore
}
]下面是一个示例片段。我想根据分数对json数组进行排序,并提取前10位(如果有的话)。
[{
name: "user1",
image: "image",
score: 10
},
{
name: "user2",
image: "image",
score: 167
},
{
name: "user3",
image: "image",
score: 1
},
{
name: "user4",
image: "image",
score: 102
},
{
name: "user5",
image: "image",
score: 12
}
]
我很难根据userScore对这个数组进行排序,以便在主板上显示前10位。任何帮助都将不胜感激。
发布于 2021-12-05 08:39:47
试着按编号排序:
const sortNum = (arr) => {
arr.sort(function (a, b) {
return a - b;
});
return arr;
};就你而言:
const sortNum = (arr) => {
arr.sort(function (a, b) {
return a.score - b.score;
});
return arr;
};https://stackoverflow.com/questions/70232587
复制相似问题