Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
sort HTMLCollection by date

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);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!