const arr1 = [33, 11, 55, 22, 66]; 
 const arr2 = [{age: 55}, {age: 22}, {age: 11}, {age: 66}, {age: 33}]
 
 console.log('排序前arr2 => ', arr2) 

 // 排序 arr2
 arr2.sort((prev, next) => {
   const p = arr1.indexOf(prev.age)
   const n = arr1.indexOf(next.age)
   return p - n
 })
 console.log('排序后arr2 => ', arr2)
 console.log('     arr1 => ', arr1)

注意这里的arr1里的元素类型必须与arr2里的类型一致,否则排序失效。

标签: none

添加新评论