I am looking to sort the values in an array based on the current date.
My values :
[{
date: "2000-12-28",
id: 1
}, {
date: "2000-11-30",
id: 2
}, {
date: "2000-09-30",
id: 3
}, {
date: "2000-05-30",
id: 4
}]
If it's November 29th :
[{
date: "2000-11-30",
id: 2
}, {
date: "2000-12-28",
id: 1
}, {
date: "2000-05-30",
id: 4
}, {
date: "2000-09-30",
id: 3
}]
I tried to do something like this, but it doesn't really work :
var array = [{id: 1, date:'2000-12-28'},
{id: 2, date:'2000-11-30'},
{id: 3, date:'2000-09-30'},
{id: 4, date:'2000-05-30'}];
const now = Date.now();
array.sort(function(a, b) {
var c = new Date(a.date);
var d = new Date(b.date);
return (now-c)-(now-d);
});
console.log(array);
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment