📌  相关文章
📜  按自定义顺序排列数组对象 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:33.608000             🧑  作者: Mango

代码示例1
var sortOrder = ['n', 'a', 'u'];
var myArray = [{
    name: 'u'
  },
  {
    name: 'n'
  },
  {
    name: 'a'
  },
  {
    name: 'n'
  }
];
myArray.sort(function(a, b) {
  return sortOrder.indexOf(a.name) - sortOrder.indexOf(b.name);
});

console.log(myArray);Save to mem.dev