答案 0 :(得分:2)
您需要做的是使用sort从a
的总和中减去b
的总和,像这样:
let arr = [
{result: 5, GMath: 5},
{result: 2, GMath: 8},
{result: 4, GMath: 10},
{result: 1, GMath: 1}
]
arr.sort((a, b) => (b.result + b.GMath) - (a.result + a.GMath))
console.log(arr)
答案 1 :(得分:1)
// const obj = { admissionStudents: {...} }; // assuming this is the object to begin with
let keys = Object.keys(obj.admissionStudents);
keys.sort((a, b) => {
return (obj.admissionStudents[b].result + obj.admissionStudents[b].GMath) - (obj.admissionStudents[a].result + obj.admissionStudents[a].GMath);
});
现在keys
将被排序,因此使用该对象构图。
新列表,
newList = [];
keys.forEach((key) => {
newList.push(obj.admissionStudents[key]);
});
newList
将成为排序列表。
希望有帮助。