假定表单数据 origin
。
const orignal = [
{ dbName: "STATUS", ... },
{ dbName: "NAME", ... },
{ dbName: "DEPART", ... },
...
]
现要将 origin
按如下优先级,针对 dbName
字段进行排序显示
"NAME", "AGE", "LEVEL", "CODE", "DEPART", "NUMBER", "STATUS"
const ruleSort = ["NAME", "AGE", "LEVEL", "CODE", "DEPART", "NUMBER", "STATUS"];
const devCardInfo = origin.sort(({ dbName: dbNameA }, { dbName: dbNameB }) => {
const iA = ruleSort.indexOf(dbNameA.toUpperCase());
const iB = ruleSort.indexOf(dbNameB.toUpperCase());
if (iA === iB) return 0;
if (iA === -1) return 1;
if (iB === -1) return -1;
return iA - iB;
});
有没有更成熟的方案?
不好意思我没有表达清楚,补充一下:
ruleSort中提及的字段并不完整,
- 未提及的字段照常显示(无需在意排序)
- 提及的字段也不一定全部存在
- 不会有重复的
dbName
这样?