How does one begin to sort this? I could not find something similar online.
I have tried to sort by length but that made it even more of a mess like so:
dateButtons.sort((a,b) => {
return b.text.length - a.text.length
})
where as "text" is equal to the date string
>Solution :
You can use the sort method and split year and month in the compare function:
const dates = ['2020 week 2', '1999 week 32', '2021 week 20', '2020 week 4', '2020 week 1'];
console.log(dates.sort((lhs, rhs) => {
const [lyear, lweek] = lhs.split(' week ');
const [ryear, rweek] = rhs.split(' week ');
return lyear - ryear || lweek - rweek;
}));
