// 对象数组按照多个字段排序
// 排序
multiSort (array, ...compairers) {
return array.sort((a, b) => {
for (const c of compairers) {
const r = c(a, b)
if (r !== 0) {
return r
}
}
})
},
let list = [
{overflow: 1, id:1, sku:1},
{overflow: 1, id:1, sku:5},
{overflow: 2, id:3, sku:2},
{overflow: 1, id:4, sku:1}
]
// 方法实用,可以按照指定字段排序,一个或者多个都可以
// 举例: 先按照overflow排序,再第一层排序的基础上再按照id排序,在第三次排序的基础上再按照sku
let list = this.multiSort(this.testList, (a, b) => a.overflow - b.overflow,
(a, b) => a.id - b.id, (a, b) => a.sku - b.sku)
console.info('list', list);
let testArr = [
{sku: '112-TEST-34', pickingRank: 19, id: 1},
{sku: '112-TEST-32', pickingRank: 19, id: 2},
{sku: '112-TEST-31', pickingRank: 19, id: 2},
{sku: '112-TEST-33', pickingRank: 20, id: 3},
{sku: '112-TEST-34', pickingRank: 19, id: 3}
]
// 转换成字母 并按照abc排序
testArr.sort((a, b) => {
return (a.sku).localeCompare(b.sku) // 正序排列
})
// testArr.sort(function(a,b){return b.sku.localeCompare(a.sku)}); //反序排列
console.info(testArr, '2222')