How to sort a div by a string attribute with jQuery?

Asked

Viewed 856 times

5

I am developing a list that has the header "ID" and "Status" and its ID and Status values,I have also defined an input of type Hidden for action.

Following example:

<div class="cabecalho">
   <div><span id="orderID">ID</span></div>
   <div><span id="orderStatus">Status</span></div>
</div>
<div id="container">
   <div class="listStatus" status="f" contagem="1">
      <div>1<div>
      <div>F<div>
   </div>

   <div class="listStatus" status="C" contagem="2">
      <div>2<div>
      <div>C<div>
   </div>
</div>
<input type="hidden" id="acao" value="asc"/>

So for the ordering to work, I wrote the following code.

$(document).ready(function(){
    $("#orderStatus").click(function(){
        var acao = $('#acao').val();
        var statusList = $(".listStatus");
        statusList.sort();
        if(acao=="desc"){
           $('#acao').val('asc');
           statusList.reverse();
        }
        else{
           $('#acao').val('desc');
        }
        $('#container').html(statusList);
    });
 });

Only the JS error console is displaying the following error: "statusList.Reverse is not a Function".

Also, 'ASC' ordering is not working.

Updating:

I got it this way, it was simpler than I expected, HTML remains the same, only Javascript changed, I will leave the code below so other people can use.

Follows code:

$("#orderStatus").click(function(){
    var acao = $('#acao').val();
    var statusList = $(".listStatus");
    statusList.sort(function(a, b) {
        if(acao == "asc"){
            $('#acao').val('desc');
            return $(b).attr("status") > $(a).attr("status");
        }
        else{
            $('#acao').val('asc');
            return $(a).attr("status") > $(b).attr("status");
        }
    });
    $('#container').html(statusList);
});
  • 1

    i think Reverse(); only works with array.

  • 1

    It didn’t really work, the alphabetical order is similar to the number order, only the sign changes from "-" to ">". Thanks for the tip!!

  • 1

    sorry I didn’t provide an answer but I didn’t have time to a googada or study about.

  • 1

    @Jasarorion, that’s right, the function reverse() exists only for array.

  • It falls into another situation, when I go to do the list I do a check, to assemble the list... if i mod 2 = 0 background color = FFF, if not DCDC background color... So when I click to do the ordering, the colors sort together, leaving the colors grouped, which actually should be with yes, color not to facilitate the visualization of the entire line. I’m not able to change the CSS of the div to be in this format, have to do it in this same function I will have to do another?

  • 4

    @Brunofolle, good afternoon. Put the resolution as answer to your own question. (Since it has been solved)

  • Maybe the answer to that question can help you https://stackoverflow.com/questions/26945484/how-to-sort-html-elements-according-to-array-values

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.