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

272
Views
Using JavaScript to sort divs based upon content & sorting empty divs to the end

The code below shows my current workings. the commented out line sorts my divs but leaves the empty divs at the top.

the 4 lines above do not make a difference

I think I'm missing something really simple here

//Group 1
    var alphabeticallyOrderedDivs7 = $('.group1').sort(function getSort(a, b) {
        if($(a).find(a).text() === "") return 1;
        if($(b).find(b).text() === "") return -1;
        if($(a).find(a).text() === $(b).find(b).text()) return 0;
        return $(a).find(a).text() < $(b).find(b).text()) ? -1 : 1;
        //return String.prototype.localeCompare.call($(a).text().toLowerCase(), $(b).text().toLowerCase());
    });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can simply do this:

//Group 1
    var alphabeticallyOrderedDivs7 = $('.group1').sort(function getSort(a, b) {
        //Assuming $(a).text() and $(b).text() is the content of the div
        return $(a).text() < $(b).text() ? 1 : $(a).text() > $(b).text() ? -1 : 0;
    });
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!