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

171
Views
How can I compare "localeCompare" numbers?

I have an error:

"Uncaught (in promise) TypeError: _post1 $ _this3 $ selecte.localeCompare"

I understand post1 gets a number as an argument, but how can I compare "localeCompare" numbers?

return [...this.posts].sort((post1, post2) => post1[this.selectedSort]?.localeCompare(post2[this.selectedSort]));
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You don't have to use localeCompare, with numbers you can just use sort((a, b) => a.x - b.x) if you have an array of objects with a number property you want to use to sort.

For example

const data = [{age: 12}, {age: 7}, {age: 18}];

const result = data.sort((a, b) => a.age - b.age);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

I temporarily fixed the problem with the if else operator but leave the issue open as I want to optimize the feature in the future

if(this.selectedSort == 'id'){
  return [...this.posts].sort((post1, post2) => post1[this.selectedSort] - post2[this.selectedSort]);
}
else{
  return [...this.posts].sort((post1, post2) => post1[this.selectedSort]?.localeCompare(post2[this.selectedSort]));
}

It worked for me !!!

return [...this.posts].sort((post1, post2) => String(post1[this.selectedSort])?.localeCompare(String(post2[this.selectedSort]), 'en', {numeric: true}));

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!