按两个条件对对象键进行排序,但优先考虑第一个

Order object keys by two conditions but give priority to the first one

我正在尝试根据三个条件对对象的键进行排序,首先是单词 Current,然后是 Conv,最后是 Traffic,但我不想要任何取消其先前的条件。

const obj = {
  'Additional Traffic': 2,
  'Current Conv': 1,
  'Additional Conv': 0.5,
  'Current Rev': 100,
  'Additional Rev': 50
}

const res = Object.keys(obj).sort((a, b) => a.includes('Current') &&  a.includes('Conv') ? -1 : 0)

// Expected output
// Current Conv  
// Current Rev 
// Additional Traffic  
// Additional Conv 
// Additional Rev  

console.log(res)

鉴于第二个单词没有按字母顺序排序,我建议创建一个 sortHash 来为单词赋予数值..

  • 这可以通过仅使用首字母并添加更多规则来改进..

  • 我也相信这只是 2 个词.. 可以改进以处理逻辑中的更多词

    const [wordA1, wordA2, wordA3, wordA4 /*...*/] = a.split(' ')

代码

const obj = {'Additional Traffic': 2,'Current Conv': 1,'Additional Conv': 0.5,'Current Rev': 100,'Additional Rev': 50,}

const sortHash = {
  word1: {
    Current: 0,
    Additional: 1,
  },
  word2: {
    Traffic: 0,
    Conv: 1,
    Rev: 2,
  },
}

const res = Object.keys(obj).sort((a, b) => {
  const [wordA1, wordA2] = a.split(' ')
  const [wordB1, wordB2] = b.split(' ')
  return (
    sortHash.word1[wordA1] - sortHash.word1[wordB1] ||
    sortHash.word2[wordA2] - sortHash.word2[wordB2]
  )
})

console.log(res)

通常你会有一个函数,它使用第一个标准进行比较,如果结果为 0(相等),你将与下一个标准进行比较。

所以对你来说是:

const obj = {
  'Additional Traffic': 2,
  'Current Conv': 1,
  'Additional Conv': 0.5,
  'Current Rev': 100,
  'Additional Rev': 50
}

const res = Object.keys(obj).sort((a, b) => {
  let result = b.indexOf('Current') - a.indexOf('Current');
  if (result === 0) {
    result = b.indexOf('Conv') - a.indexOf('Conv');
  }
  return result;
});

// Expected output
// Current Conv  
// Current Rev 
// Additional Traffic  <-- need one more rule for this one to be sorted properly
// Additional Conv 
// Additional Rev  

console.log(res)

请注意,如果您要检查的单词很多,您可以使用这样的数组:

const obj = {
  'Additional Traffic': 2,
  'Current Conv': 1,
  'Additional Conv': 0.5,
  'Current Rev': 100,
  'Additional Rev': 50
}

const SORT_ARRAY = ['Current', 'Traffic', 'Conv'];

const res = Object.keys(obj).sort((a, b) => {
  let sortCriteriaIdx = 0;
  let result = 0;
  while (result === 0 && sortCriteriaIdx < SORT_ARRAY.length) {
    const criteria = SORT_ARRAY[sortCriteriaIdx];
    result = b.indexOf(criteria) - a.indexOf(criteria);
    sortCriteriaIdx++;
  }
  return result;
});

// Expected output
// Current Conv  
// Current Rev 
// Additional Traffic
// Additional Conv 
// Additional Rev  

console.log(res)

const obj = {
  'Additional Traffic': 2,
  'Current Conv': 1,
  'Additional Conv': 0.5,
  'Current Rev': 100,
  'Additional Rev': 50
}

    const data = Object.keys(obj).sort((key1, key2) => {
        const key1ContainsCurrent = key1.includes('Current');
        const key2ContainsCurrent = key2.includes('Current');
        const key1ContainsConv = key1.includes('Conv');
        const key2ContainsConv= key2.includes('Conv');
        debugger;
        if (key1ContainsCurrent || ((!key1ContainsCurrent && !key2ContainsCurrent) && key1ContainsConv)) {
            return -1
        }
        if (key2ContainsCurrent || key2ContainsConv) {
           return 1
        }
        return 0;

    });
    
console.log(data);