排序,数组对象排序
数组对象排序法一:
let getTableData = JSON.parse(JSON.stringify(res.data.list))
let compare = function (property) {
return function (obj1, obj2) {
let val1= obj1[property]
let val2 = obj2[property]
return val2 - val1 // 降序
}
}
getTableData.sort(compare('createTime'))
console.log(getTableData)
数组对象排序法二:
var compare1 = function (prop) {
return function (obj1, obj2) {
var val1 = obj1[prop]
var val2 = obj2[prop]
if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
val1 = Number(val1)
val2 = Number(val2)
}
// 降序
if (val1 < val2) {
return -1
} else if (val1 > val2) {
return 1
} else {
return 0
}
}
}
数组值排序:
var arr = [23, 9, 4, 78, 3];
var compare = function (x, y) { //比 较函数
if (x < y) {
return -1;
} else if (x > y) {
return 1;
} else {
return 0;
}
}
console.log(arr.sort(compare));