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'}]