【问题标题】:sort a complex structure array of objects对对象的复杂结构数组进行排序
【发布时间】:2022-11-15 06:31:11
【问题描述】:

我有这样一组卡片对象:

const cards = [
  {
    type: { method: 'listen' },
    reference: ['destroyed', 'word 2']
  },
  {
    type: { method: 'synonym' },
    reference: ['destroyed']
  },
  {
    type: { method: 'listen' },
    reference: ['destroyed']
  },
  {
    type: { method: 'dictate' },
    reference: ['destroyed']
  }
]

我想按如下方式对它们进行排序:

  1. listen卡片应该放在第一位
  2. 在收听卡之间有较少的reference.length 应该早点来
  3. 其他卡片应该会出现
  4. dictate卡片终于来了

    除了第一个条件,我不知道如何进行如此复杂的排序:

        cards.sort(compare);
    
        function compare(a, b) {
            if(a.type.method == 'listen') return a.reference.length - b.reference.length;
            ...
        }
    

【问题讨论】:

  • “然后应该有其他卡片出现” - 意思是他们的顺序没有改变?
  • 对,就是这样........

标签: javascript


【解决方案1】:

写下你想做一些“特别”的事情的所有情况:

const cards = [
  {
    type: { method: 'listen' },
    reference: ['destroyed', 'word 2']
  },
  {
    type: { method: 'synonym' },
    reference: ['destroyed']
  },
  {
    type: { method: 'listen' },
    reference: ['destroyed']
  },
  {
    type: { method: 'dictate' },
    reference: ['destroyed']
  }
];

cards.sort((a, b) => {
    // both types are 'dictate' - no change
    if (a.type.method === "dictate" && b.type.method === "dictate") return 0;
    // move `b` up since `a` is 'dictate'
    if (a.type.method === "dictate") return 1;
    // move `a` up since `b` is 'dictate'
    if (b.type.method === "dictate") return -1;
    
    // if both are 'listen' then order based on refs
    if (a.type.method === "listen" && b.type.method === "listen") return a.reference.length - b.reference.length;
    // move `a` up since `a` is 'listen'
    if (a.type.method === "listen") return -1;
    // move `b` up since `b` is 'listen'
    if (b.type.method === "listen") return 1;

    // no change
    return 0;
});

console.log(cards);
.as-console-wrapper { max-height: 100% !important }

【讨论】:

    【解决方案2】:

    const cards = [
      {
        type: { method: 'listen' },
        reference: ['destroyed', 'word 2']
      },
      {
        type: { method: 'synonym' },
        reference: ['destroyed']
      },
      {
        type: { method: 'listen' },
        reference: ['destroyed']
      },
      {
        type: { method: 'dictate' },
        reference: ['destroyed']
      }
    ]
    
    const sortedCards = cards.sort((a, b) => {
      const priority = ['listen', 'synonym', 'dictate'];
      const indexA = priority.indexOf(a.type.method);
      const indexB = priority.indexOf(b.type.method);
      
      // First try to compare against the type
      // If the types are equal, it will be eval to 0 from (indexA - indexB).
      // 0 is considered false in javascript and hence will evulate the length of the reference.
      return indexA - indexB || a.reference.length - b.reference.length;
    });
    
    console.log(sortedCards);

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多