JavaScript 对数组的数组进行排序

我想根据数组对数组数组进行排序,然后按数组中项目的长度进行排序。


采用以下主排序数组:


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);

但是,这只考虑了数组中的第一项,而不考虑其余项的排序。


达令说
浏览 118回答 2
2回答

跃然一笑

要排序,您必须首先检查长度。如果两者相等,我们必须检查a/b中第一个元素的索引位置key。如果它们相同,则继续移动到两个数组中的下一个元素。这个答案利用了0一个虚假价值的事实。例子是:0 || -1 //=> -1和1 || -1 //=> 1const key = ["meraki", "active directory", "sophos"];const raw = [  ["meraki"],  ["active directory"],  ["sophos", "active directory"],  ["active directory", "sophos"],  ["sophos"],  ["meraki", "active directory", "sophos"],];raw.sort((a, b) => (  a.length - b.length || a.reduce((diff, _, i) => (    diff || key.indexOf(a[i]) - key.indexOf(b[i])  ), 0)));console.log(raw);console.table(raw); // check browser console

慕雪6442864

考虑以下方法const key = [  "meraki",  "active directory",  "sophos"]const raw = [   ["sophos"],   ["meraki"],   ["active directory"],   ["sophos", "active directory"],   ["active directory", "sophos"],   ["meraki", "active directory", "sophos"]]const compareThis = (a, b) => {  if (a.length !== b.length) {    return a.length - b.length  }  let itemFound = 0;  for (let keyIndex in key) {    for (let aIndex in a ) {      if(a[aIndex] === key[keyIndex]) {        itemFound = -1;        break;      }      if(b[aIndex] === key[keyIndex]) {        itemFound = 1;        break;      }    }    if(itemFound !== 0) { break }  }  return itemFound;}const sortedData = raw.sort(compareThis)console.log(sortedData)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript