我想根据数组对数组数组进行排序,然后按数组中项目的长度进行排序。
采用以下主排序数组:
const key = [
"meraki",
"gsuite",
"active directory",
"sophos",
"manageengine"
]
我想采用类似于以下内容的数组:
const raw = [
["manageengine"],
["sophos"],
["active directory"],
["gsuite"],
["meraki"],
["sophos", "manageengine"],
["active directory", "sophos"],
["active directory", "manageengine"],
["gsuite", "active directory"],
["gsuite", "sophos"],
["gsuite", "manageengine"],
["meraki", "gsuite"],
["meraki", "active directory"],
["meraki", "sophos"],
["meraki", "manageengine"],
["active directory", "sophos", "manageengine"],
["gsuite", "active directory", "sophos"],
["gsuite", "active directory", "manageengine"],
["gsuite", "sophos", "manageengine"],
["meraki", "gsuite", "active directory"],
["meraki", "gsuite", "sophos"],
["meraki", "active directory", "sophos"],
["meraki", "gsuite", "manageengine"],
["meraki", "active directory", "manageengine"],
["meraki", "sophos", "manageengine"],
["gsuite", "active directory", "sophos", "manageengine"],
["meraki", "gsuite", "active directory", "sophos"],
["meraki", "gsuite", "active directory", "manageengine"],
["meraki", "gsuite", "sophos", "manageengine"],
["meraki", "active directory", "sophos", "manageengine"],
["meraki", "gsuite", "active directory", "sophos", "manageengine"]
];
在上面的示例中,我希望raw根据key数组中的每个项目对数组进行排序。我的第一次尝试是做类似的事情:
const result = [];
for (const name of result) {
const sorted = keys.filter((s) => s[0] === name);
result.push(...sorted);
}
result.sort((a, b) => a.length - b.length);
但是,这只考虑了数组中的第一项,而不考虑其余项的排序。
跃然一笑
慕雪6442864
相关分类