如果想按照value值进行排序,可以使用Array.prototype.sort()
方法,代码如下:
let arr = [{name:"ddd",value:10},{name:"aaa",value:20}];
arr.sort((a, b) => b.value - a.value);
console.log(arr); // [{name:"aaa",value:20},{name:"ddd",value:10}]
如果想按照value值进行排序,可以使用Array.prototype.sort()
方法,代码如下:
let arr = [{name:"ddd",value:10},{name:"aaa",value:20}];
arr.sort((a, b) => b.value - a.value);
console.log(arr); // [{name:"aaa",value:20},{name:"ddd",value:10}]