1、排序
var arr = [
{ deviceNO: 'A123', age: 25 },
{ deviceNO: 'A345', age: 25 },
{ deviceNO: 'A123', age: 20 },
{ deviceNO: 'A345', age: 30 }
];
function createComparisonFunction(propertyName) {
return function (object1,object2){
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1 < value2){
return -1;
} else if (value1 > value2){
return 1;
}else{
return 0
}
}
}
arr.sort(createComparisonFunction("age"))
console.log(arr)
2、表格中排序
<u-table-column v-if="item.show" :key="item + index" :label="langObj[item.columnCode]" :prop="item.labelCode"
:align="item.alignType" :width="parseInt(item.thWidth)" :sortable="true"
:sort-method="(a, b) => sortMethod(a, b, item.columnCode, item.alignType)">
<template slot-scope="scope">
<span :class="spanClass(scope.row, item)">{{ scope.row[item.labelCode] }}</span>
</template>
</u-table-column>
sortMethod(a, b, prop, align) {
if (align == "right") {
let at = a[prop] == "-" ? "0" : a[prop];
let bt = b[prop] == "-" ? "0" : b[prop];
return at - bt;
} else {
if (a[prop] < b[prop]) {
return -1
} else if (a[prop] > b[prop]) {
return 1
}
}
},