I have an HTMLCollection of div that i would like to sort by date and it return an error that the.sort on my collection is not a function
const date = new Date();
const stages = document.getElementsByClassName("cadre-affiches");
for (let i = 1; i < stages.length; i++) {
stages[i].dateStage = new Date(stages[i].getAttribute("date"));
if (stages[i].dateStage < date) {
stages[i].remove();
}
};
let sorted = stages.sort((a,b) => b.dateStage - a.dateStage);
console.log(sorted);
document.getElementsByClassName return HTMLCollection which is an Object. You can convert it to an Array to access the sort function.
Replace this line of code.
let sorted = stages.sort((a,b) => b.dateStage - a.dateStage);
to this
let sorted = Array.from(stages).sort((a,b) => b.dateStage - a.dateStage);