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

157
Views
Javascript - Sort does not work properly with string

I am new to JS and trying to sort an array based on a key of the object (each object is an element in the array). I am not sure why the following code does not sort the array properly. I have checked the doc carefully and the code looks correct to me. I tried to replace the date value with numerical numbers and it worked. Not sure what is wrong with the date string.

var dic=[{key: '2022-05-13'}, {key: '2022-05-06'}]
dic.sort(function (a, b) {
  return (a.key - b.key);
})
console.log(dic)

Output

[{key: '2022-05-13'}, {key: '2022-05-06'}]

I thought the output should be

[ {key: '2022-05-06'}, {key: '2022-05-13'}]
about 4 years ago · Santiago Gelvez
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!