Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!