Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
How to sort array of object with a condition?
    let array = [
  {
    "vendorBidId": null,
    "participantName": "test+10@mail.com",
    "bidAmount": 0,
    "productionRate": null,
    "bidTime": null,
    "isYou": false,
    "awarded": false
  },
  {
    "vendorBidId": 16,
    "participantName": "test+2@mail.com",
    "bidAmount": 131,
    "productionRate": 131,
    "bidTime": "2021-09-18T08:11:21.295",
    "isYou": true,
    "awarded": false
  },
  {
    "vendorBidId": 20,
    "participantName": "test@mail.com",
    "bidAmount": 30,
    "productionRate": 30,
    "bidTime": "2021-0a-18T08:11:21.295",
    "isYou": true,
    "awarded": false
  },
  {
    "vendorBidId": 30,
    "participantName": "test@mail.com",
    "bidAmount": 40,
    "productionRate": 40,
    "bidTime": "2021-0a-18T08:11:21.295",
    "isYou": true,
    "awarded": false
  },
  {
    "vendorBidId": null,
    "participantName": "test+10@mail.com",
    "bidAmount": 0,
    "productionRate": null,
    "bidTime": null,
    "isYou": false,
    "awarded": false
  },
  {
    "vendorBidId": 40,
    "participantName": "test@mail.com",
    "bidAmount": 50,
    "productionRate": 50,
    "bidTime": "2021-0a-18T08:11:21.295",
    "isYou": true,
    "awarded": false
  }
]

I want to sort this array in ascending order(bidAmount), and if the vendorBidId is null don't sort keep in the bottom.

I tried like this

array.sort((a,b) => parseFloat(a.bidAmount) - parseFloat(b.bidAmount)).map((ele) => console.log(ele.bidAmount))

I'm need an out like 30,40,50,131,0,0, but for the my above code it will just sort only. I tried with reduce but that does't work properly

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

array.sort((a,b) => {
  const n = a.vendorBidId === null ? Number.MAX_VALUE : a.bidAmount;
  const m = b.vendorBidId === null ? Number.MAX_VALUE : b.bidAmount;
  return n - m; 
});
// "2.5" + "3.5" = "2.53.5"
// "2.5" - "3.5" = -1 
about 4 years ago · Juan Pablo Isaza Relatório

0

  1. Separate your arrays based in vendorBidId being null or not.
  2. Sort the part you want to sort
  3. concat the other array.
about 4 years ago · Juan Pablo Isaza Relatório

0

Building on what DragatS has shared.. just map through the result of the sort() like this to get the result of [30,40,50,131,0,0]

const res = array
.sort((a, b) => {
    const n = a.vendorBidId === null ? Number.MAX_VALUE : a.bidAmount;
    const m = b.vendorBidId === null ? Number.MAX_VALUE : b.bidAmount;
    return n - m;
})
.map(item => item.bidAmount);
console.log(res);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda