4 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
let students = [
{
name:"張三",
score:98
},
{
name:"李四",
score:65
},
{
name:"王五",
score:75
}
];
students.sort((a,b)=>{
return b.score-a.score;
});
輸出:
[
{name: "張三", score: 98},
{name: "王五", score: 75},
{name: "李四", score: 65}
]
更新
//使用split("||")可以分割"||"兩側(cè)字符串,得到一個(gè)數(shù)組。數(shù)組第一項(xiàng)為姓名,第二項(xiàng)為分?jǐn)?shù)
//按分?jǐn)?shù)排序
score.sort((a,b)=>{
scoreA = +a.split("||")[1];
scoreB = +b.split("||")[1];
return scoreB - scoreA;
});
//輸出姓名和分?jǐn)?shù)
for(var i = 0;i < score.length;i++){
//姓名
console.log(score[i].split("||")[0]);
//分?jǐn)?shù)
console.log(score[i].split("||")[1]);
}
這個(gè)問題其實(shí)挺基礎(chǔ)的,提問前先百度吧~

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
var testList=[......]
function sortList(a, b) {
return b.score - a.score
}
testList.sort(sortList)
添加回答
舉報(bào)