Skip to content
Advertisement

Javascript – Sort array of objects by property date

I have the following response from a service:

JavaScript

To sort it by date I do the following:

JavaScript

But the value of “sortDate” is not correct:

JavaScript

Should be:

JavaScript

How can i solve it?, thanks

Advertisement

Answer

The problem is that the comparison function is supposed to return a positive number for if a should be after b, negative if b should be after a, and zero if they should keep the same order. Your function is returning true and false, JS converts true to 1 and false to zero. That means you are never telling sort to put b after a.

have the function return

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement