1 sort
console.log([{id: 1}, {id: 3}, {id: 2}].sort((a,b) => {return a.id - b.id}))
2 ES6
const compare = (key) => {
return (obj1, obj2) => {
let value1 = obj1[key]
let value2 = obj2[key]
if (value1 < value2) {
return 1;
} else if (value1 > value2) {
return -1;
} else {
return 0
}
}
}
let data = [
{ label: '一一一', value: 111},
{ label: '三三三', value: 333},
{ label: '二二二', value: 222},
]
console.log(data.sort(compare(`value`)))