为了账号安全,请及时绑定邮箱和手机立即绑定

JavaScript 对数组的数组进行排序

JavaScript 对数组的数组进行排序

达令说 2023-05-25 16:37:05
我想根据数组对数组数组进行排序,然后按数组中项目的长度进行排序。采用以下主排序数组: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);但是,这只考虑了数组中的第一项,而不考虑其余项的排序。
查看完整描述

2 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

要排序,您必须首先检查长度。如果两者相等,我们必须检查a/b中第一个元素的索引位置key。如果它们相同,则继续移动到两个数组中的下一个元素。


这个答案利用了0一个虚假价值的事实。例子是:0 || -1 //=> -1和1 || -1 //=> 1


const 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


查看完整回答
反对 回复 2023-05-25
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

考虑以下方法


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)


查看完整回答
反对 回复 2023-05-25
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信