// 要排序的对象数组
const arr1 = [
{ code: 3, name: 'C' },
{ code: 1, name: 'A' },
{ code: 2, name: 'B' }
];
// 按照此顺序排序的对象数组
const arr2 = [
{ code: 1, order: 'aaa' },
{ code: 2, order: 'bbb' },
{ code: 3, order: 'ccc' }
];
this.sortList(arr2 , arr1 )
排序方法:
// 排序
sortList(list, needSortList) {
const _list = list.map(li => li.code)
return needSortList.sort((a, b) => {
return _list.indexOf(a.code) < _list.indexOf(b.code) ? -1 : 1
})
},