首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按多个字段排序数组,但具有优先级字段的项先排序

按多个字段排序数组,但具有优先级字段的项先排序
EN

Stack Overflow用户
提问于 2022-05-03 20:35:21
回答 2查看 205关注 0票数 0

我有一个小的排序算法,它按多个字段对数组进行排序:

代码语言:javascript
复制
const termKeys = Object.keys(proposal.terms);

const sorted = termKeys.sort((a, b) => {
  if (proposal.terms[a].unified_price === proposal.terms[b].unified_price) {
    return gates[b].productivity - gates[a].productivity;
  }

  return proposal.terms[a].unified_price - proposal.terms[b].unified_price;
});

我想在这个排序算法中添加一个额外的可选参数,它将保持顺序,但首先放置具有特殊字段的项。例如,以下列表:

代码语言:javascript
复制
const proposal = {
  terms: {
    '1': {
      unified_price: 123,
      productivity: 10,
      has_baggage: false,
    },
    '2': {
      unified_price: 111,
      productivity: 10,
      has_baggage: false,
    },
    '3': {
      unified_price: 456,
      productivity: 10,
      has_baggage: true,
    },
    '4': {
      unified_price: 678,
      productivity: 10,
      has_baggage: true,
    }
  }
}

应按以下顺序排列

代码语言:javascript
复制
['3', '4', '2', '1']

我尝试在公式中添加一个额外的函数和一个数字,但这并不影响顺序:

代码语言:javascript
复制
const hasBaggage = true;
const proposal = {
  terms: {
    '1': {
      unified_price: 123,
      productivity: 10,
      has_baggage: false,
    },
    '2': {
      unified_price: 111,
      productivity: 10,
      has_baggage: false,
    },
    '3': {
      unified_price: 456,
      productivity: 10,
      has_baggage: true,
    },
    '4': {
      unified_price: 678,
      productivity: 10,
      has_baggage: true,
    }
  }
}

function decidePositionOfBaggage(a, b) {
  if (a === b) {
    return 0;
  }

  if (b === false && a === true) {
    return -1;
  }

  if (b === true && a === false) {
    return 1;
  }
}

function sortProposal(proposal) {
  const termKeys = Object.keys(proposal.terms);

  const sorted = termKeys.sort((a, b) => {
    const hasBaggageNumber = decidePositionOfBaggage(proposal.terms[a].has_baggage, proposal.terms[b].has_baggage);
    
    if (proposal.terms[a].unified_price === proposal.terms[b].unified_price) {
      return (gates[b].productivity - gates[a].productivity) - hasBaggageNumber;
    }
  
    return (proposal.terms[a].unified_price - proposal.terms[b].unified_price) - hasBaggageNumber;
  });

  return sorted;
}

console.log(sortProposal(proposal))

EN

回答 2

Stack Overflow用户

发布于 2022-05-03 21:06:47

您的代码可以大规模简化为以下内容:

代码语言:javascript
复制
const proposal = {
    terms: {
        1: {
            unified_price: 123,
            productivity: 10,
            has_baggage: false,
        },
        2: {
            unified_price: 111,
            productivity: 10,
            has_baggage: false,
        },
        3: {
            unified_price: 456,
            productivity: 10,
            has_baggage: true,
        },
        4: {
            unified_price: 678,
            productivity: 10,
            has_baggage: true,
        },
    },
};

const sort = ({ terms }) => {
    return Object.entries(terms).sort(([_, a], [__, b]) => {
        if (a.has_baggage && !b.has_baggage) return -1;
        if (!a.has_baggage && b.has_baggage) return 1;

        return a.unified_price - b.unified_price;
    });
};

console.log(sort(proposal));

票数 1
EN

Stack Overflow用户

发布于 2022-05-03 21:20:18

你想像这样做分类:

代码语言:javascript
复制
const hasBaggage = true
const proposal = {
  terms: {
    '1': {
      unified_price: 123,
      productivity: 10,
      has_baggage: false,
    },
    '2': {
      unified_price: 111,
      productivity: 10,
      has_baggage: false,
    },
    '3': {
      unified_price: 456,
      productivity: 10,
      has_baggage: true,
    },
    '4': {
      unified_price: 678,
      productivity: 10,
      has_baggage: true,
    }
  }
}

function decidePositionOfBaggage(a, b) {
  if (a === b) {
    return 0;
  }

  if (b === false && a === true) {
    return -1;
  }

  if (b === true && a === false) {
    return 1;
  }
}

function sortProposal(proposal) {
  const termKeys = Object.keys(proposal.terms);

  const sorted = termKeys.sort((a, b) => {
    const hasBaggageNumber = decidePositionOfBaggage(proposal.terms[a].has_baggage, proposal.terms[b].has_baggage);

    if (hasBaggageNumber !== 0) {
      return hasBaggageNumber
    }
    if (proposal.terms[a].unified_price === proposal.terms[b].unified_price) {
      return (gates[b].productivity - gates[a].productivity);
    }

    return (proposal.terms[a].unified_price - proposal.terms[b].unified_price);
  });

  return sorted;
}

console.log(sortProposal(proposal))

主要的变化是,基本上您只需要先检查具有优先级的属性,然后在两个项之间的优先级相同时检查其他属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72105183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档