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

293
Views
How can I skip a string during sorting in js

I am trying to sort the merit list from my array of objects, but on my object, there is a string "Failed", I want to skip it during the sorting but when sorting is done the failed merit should be added at the end

const markSheet = [{ merit: 1 }, { merit: "Failed" }, { merit: 2 }];
const defineMerit = (markSheet) => {
  return markSheet
    .filter((item) => item.merit !== "Failed")
    .sort((a, b) => a.merit - b.merit);
};
console.log(defineMerit(markSheet))

example:

[
  {
    "merit": 1
  },
  {
    "merit": 2
  },
  {
    "merit": "Failed"
  }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Put the failed check inside the sort callback (compare the difference of whether an item is Failed or not)

const markSheet = [{ merit: 1 }, { merit: "Failed" }, { merit: 2 }];
const defineMerit = (markSheet) => {
  return markSheet
    .sort((a, b) => (
      ((a.merit === 'Failed') - (b.merit === 'Failed'))
      || a.merit - b.merit
    ));
};
console.log(defineMerit(markSheet))

or reverse the filter condition afterwards and combine with the sorted array

const markSheet = [{ merit: 1 }, { merit: "Failed" }, { merit: 2 }];
const defineMerit = (markSheet) => {
  const sorted = markSheet
    .filter((item) => item.merit !== "Failed")
    .sort((a, b) => a.merit - b.merit);
  return sorted.concat(
    markSheet.filter((item) => item.merit === "Failed")
  );
};
console.log(defineMerit(markSheet))

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!