skip to Main Content

i use a javascript routine with jquery3.6.1 to order divs as follow when the button #tri is clicked:

        $("#tri").click(function() {
        var $divs = $("#resultats div.notice");
        var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
            return $(a).find("div.bibloc:first").text() > $(b).find("div.bibloc:first").text();
        });
        $("#resultats").html(alphabeticallyOrderedDivs);
    
    });

the divs are constructed like this (inside the #resultat div):

    <div class='notice' class='2022'>
        <div class='internotice'></div>
        <div class='notice_header'>
            <p class='title'><img src='img1.png' /></p>
            <h4>Title1</h4>
            <p>details</br>info</p>
        </div>
        <div class='bibloc'>
            <p class='enrayon'> ref1<span>text</span></p>
        </div>
    </div>
    
    <div class='notice' class='2022'>
        <div class='internotice'></div>
        <div class='notice_header'>
            <p class='title'><img src='img1.png' /></p>
            <h4>Title2</h4>
            <p>details</br>info</p>
        </div>
        <div class='bibloc'>
            <p class='enrayon'> ref2<span>text</span></p>
        </div>
    </div>

it works perfectly in firefox, all the divs are sorted with "ref1,ref2…"

no other browser i tested want to do the sort.
no error message in console, just doesn’t work

any idea of what could be the source of this problem ?
thanks

tried everything, change or simplify html, move javascript function but nothing work

2

Answers


  1. Chosen as BEST ANSWER

    thanks Peter Thoeny !!!!

    the solution was effectively :

    return ($(a).find("div.bibloc:first").text()) > ($(b).find("div.bibloc:first").text()) ? 1 : -1;
    

  2. For sort, be specific, and return 1 or -1:

    return ($(a).find("div.bibloc:first").text()) > ($(b).find("div.bibloc:first").text()) ? 1 : -1;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search