I am not able to sort by latest date time in javascript.
Below is my json :
let jsonStr = [{id:1,date:"04/04/2017 17:47"},
{id:2,date:"05/01/2015 12:35"},
{id:3,date:"31/02/2017 10:00"},
{id:4,date:"31/02/2017 12:00"}];
Expected result : Latest date time should be first in the list
- 04/04/2017 17:47
- 31/02/2017 12:00
- 31/02/2017 10:00
- 05/01/2015 12:35
Current result :
- 31/02/2017 12:00
- 31/02/2017 10:00
- 04/04/2017 17:47
- 05/01/2015 12:35
Sorting logic that I used :
function sortFunction(a,b){
var dateA = new Date(a.date).getTime();
var dateB = new Date(b.date).getTime();
return dateB > dateA ? 1 : -1;
};
let jsonStr = [{id:1,date:"04/04/2017 17:47"},
{id:2,date:"05/01/2015 12:35"},
{id:3,date:"31/02/2017 10:00"},
{id:4,date:"31/02/2017 12:00"}];
jsonStr.sort(sortFunction);
Reference links that I used :
- Sort Javascript Object Array By Date
- Sorting an array based on time stamp value from JSON Response
- Sort Javascript Object Array By Date
Thanks in advance.