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

196
Views
get data attribute from array

i am tring to modify this sortlist to sort by most recent date from a data attribute.

function sortList(ul) {
   var ul = document.getElementById(ul);
   Array.from(ul.getElementsByTagName("div"))
   .sort((a, b) => a.textContent.localeCompare(b.textContent))
   .forEach(div => ul.appendChild(div));
}

and here is the html

 <div class="grid customer-card-cnt" id="customerlist">
     <div class="col col-sm col-4" data-date="2014-05-10"></div>
     <div class="col col-sm col-4" data-date="2022-05-10"></div>
     <div class="col col-sm col-4" data-date="2021-05-10"></div>
</div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems like you're sorting using the wrong property i.e. textContent instead of the value of the data-date attribute. I've modified your example below to show the correct sort function.

function sortList(ul) {
  var ul = document.getElementById(ul);
  Array.from(ul.getElementsByTagName("div"))
    .sort((a, b) => b.dataset.date.localeCompare(a.dataset.date))
    .forEach((div) => ul.appendChild(div));
}

This should produce the following html:

 <div class="grid customer-card-cnt" id="customerlist">
     <div class="col col-sm col-4" data-date="2022-05-10"></div>
     <div class="col col-sm col-4" data-date="2021-05-10"></div>
     <div class="col col-sm col-4" data-date="2014-05-10"></div>
</div>
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!