The way I think, this could be done easily using split()
to extract each day, month, year, and then, use those values to construct Dates objects. Finally, you compare and sort those dates.
const splitDatesByComma = dates.split(',').map((el) => el.trim())
const dates = splitDatesByComma.map((el) => {
const splitDate = el.split('/')
// Create a Date for splitted string. Month is 0 based
return new Date(splitDate[2], splitDate[1] - 1, splitDate[0], 0, 0, 0, 0)
})
const sortDatesDescending = dates.sort((dateA, dateB) => {
if (dateA > dateB) {
return -1
}
if (dateA < dateB) {
return 1
}
return 0
})
// Format sorted dates to string and join them.
const datesFormattedAndSorted = sortDatesDescending.map((date) => {
return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`
})
console.log(datesFormattedAndSorted.join(', '))
I've done this at CodePen with Vue if anyone is interested: https://codepen.io/LucasFer/pen/mdMmqrN