I am looking to sort the values in an array based on the current date.
My values :
[{
date: "12-28",
id: 1
}, {
date: "11-30",
id: 2
}, {
date: "09-30",
id: 3
}, {
date: "05-30",
id: 4
}]
I tried to do something like this, but it doesn't really work :
var array = [{id: 1, date:'12-28'},
{id: 2, date:'11-30'},
{id: 3, date:'09-30'},
{id: 4, date:'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);
If it's November 29th :
[{
date: "11-30",
id: 2
}, {
date: "12-28",
id: 1
}, {
date: "05-30",
id: 4
}, {
date: "09-30",
id: 3
}]
CodePudding user response:
You can use Array.sort()
to give you the desired result, I've moved the dates in the input array to this year:
let input = [{ date: "2021-12-28", id: 1 }, { date: "2021-11-30", id: 2 }, { date: "2021-09-30", id: 3 }, { date: "2021-05-30", id: 4 }];
function sortByNearest({ date: a}, {date: b}) {
const now = Date.now();
return (Math.abs(Date.parse(a) - now)) - (Math.abs(Date.parse(b) - now));
}
console.log(input.sort(sortByNearest))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
How about turning mm-dd
to mmdd
and then converting to number and then using Math.abs()
as follows?
const arr = [
{date: "12-28",id: 1},
{date: "11-30",id: 2},
{date: "09-30",id: 3},
{date: "05-30",id: 4}
];
const dt = new Date();
const now = [dt.getMonth() 1, dt.getDate()].map(v => `0${v}`.slice(-2)).join('');
const ordered = arr.sort((a,b) => Math.abs( a.date.replace(/[^\d] /,'') - now) - Math.abs( b.date.replace(/[^\d] /,'') - now));
console.log( ordered );
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>