I'm wanting to sort an array of arrays based on a array and then by length of items in the array.
Take the following master sort array:
const key = [
"meraki",
"gsuite",
"active directory",
"sophos",
"manageengine"
]
I want to take a array that resembles:
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"]
];
In the above example, I want the raw array to be sorted by accordingly to each item in the key array. My first attempt was to do something like:
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);
However that only takes into account the first item in the array, not the sort of the rest of the items.