Sort list of strings by Year and Month

Viewed 18

I have a list of strings

const mylist = ["October 2020","December 2022","November 2020" ,...etc] 

I would like to sort by year and month. How can I go about this without using external libraries/modules?

1 Answers

Have you tried sorting?

const mylist = ["October 2020", "December 2022", "November 2020"]
const sorted = [...mylist].sort((a, b) => new Date(a) - new Date(b))
console.log(sorted)

Related