0: Object { gradeName: "A+", gradeMaxMarks: "89", gradeMinMarks: "80" }
1: Object { gradeName: "A", gradeMaxMarks: "79", gradeMinMarks: "70" }
2: Object { gradeName: "B+", gradeMaxMarks: "69", gradeMinMarks: "60" }
3: Object { gradeName: "B", gradeMaxMarks: "59", gradeMinMarks: "55" }
4: Object { gradeName: "C", gradeMaxMarks: "49", gradeMinMarks: "40" }
5: Object { gradeName: "D", gradeMaxMarks: "39", gradeMinMarks: "30" }
6: Object { gradeName: "A++", gradeMaxMarks: "100", gradeMinMarks: "90" }
length: 7
我在JavaScript中有上面的对象,它没有正确排序。我该如何解决这个问题?
答案 0 :(得分:0)
var arr = [{
gradeName: "A+",
gradeMaxMarks: "89",
gradeMinMarks: "80"
},
{
gradeName: "A",
gradeMaxMarks: "79",
gradeMinMarks: "70"
},
{
gradeName: "B+",
gradeMaxMarks: "69",
gradeMinMarks: "60"
},
{
gradeName: "B",
gradeMaxMarks: "59",
gradeMinMarks: "55"
},
{
gradeName: "C",
gradeMaxMarks: "49",
gradeMinMarks: "40"
},
{
gradeName: "D",
gradeMaxMarks: "39",
gradeMinMarks: "30"
},
{
gradeName: "A++",
gradeMaxMarks: "100",
gradeMinMarks: "90"
}
]
arr.sort((a,b) => Number(b.gradeMaxMarks)-Number(a.gradeMaxMarks))
console.log(arr)
答案 1 :(得分:0)
您似乎忘记了脚本的sort
部分。
const input = [{
gradeName: "A+",
gradeMaxMarks: "89",
gradeMinMarks: "80"
},
{
gradeName: "A",
gradeMaxMarks: "79",
gradeMinMarks: "70"
},
{
gradeName: "B+",
gradeMaxMarks: "69",
gradeMinMarks: "60"
},
{
gradeName: "B",
gradeMaxMarks: "59",
gradeMinMarks: "55"
},
{
gradeName: "C",
gradeMaxMarks: "49",
gradeMinMarks: "40"
},
{
gradeName: "D",
gradeMaxMarks: "39",
gradeMinMarks: "30"
},
{
gradeName: "A++",
gradeMaxMarks: "100",
gradeMinMarks: "90"
}
];
input.sort((a, b) => b.gradeMaxMarks - a.gradeMaxMarks);
console.log(input);

答案 2 :(得分:0)
使用Javascript的Array.sort()方法对降序 [100,80,60,40,....](您的要求)中的数组进行排序,如下所示:< / p>
array.sort(function(a, b) {
return b.gradeMaxMarks - a.gradeMaxMarks;
});
如果你想在升序 [10,20,40,45,60 ......]中对数组进行排序,那就这样做:
array.sort(function(a, b) {
return a.gradeMaxMarks - b.gradeMaxMarks;
});
以下是工作代码段:
var array = [{gradeName:"A+",gradeMaxMarks:"89",gradeMinMarks:"80"},{gradeName:"A",gradeMaxMarks:"79",gradeMinMarks:"70"},{gradeName:"B+",gradeMaxMarks:"69",gradeMinMarks:"60"},{gradeName:"B",gradeMaxMarks:"59",gradeMinMarks:"55"},{gradeName:"C",gradeMaxMarks:"49",gradeMinMarks:"40"},{gradeName:"D",gradeMaxMarks:"39",gradeMinMarks:"30"},{gradeName:"A++",gradeMaxMarks:"100",gradeMinMarks:"90"}];
array.sort(function(a, b) {
return b.gradeMaxMarks - a.gradeMaxMarks;
});
console.log(array)
&#13;