Linked Questions

Popular Questions

How can I sort items correctly using js

Asked by At

I searched problems similar to this one, but those didn't solve my problem. I have a bootstrap cards showing items. And i have a filter to order this cards by liked votes, but when I trigger the js function, the sort doesnt works well (looks like it's ordering in a random way) .

html:

 <a class="like badge" onclick="vote('{% url 'like_item' pk=item.pk %}', '.liked_item_{{item.id}}', '.disliked_item_{{item.id}}', '.like_{{item.id}}', '.dislike_{{item.id}}')" href="javascript:void(0)">
        <span class="liked_item_{{item.id}} liked_votes">
          {{ item.count_likes }}
        </span>
      </a>

js:

if(this.name == 'thumbsUp'){
    var thumbsUpOrderedDivs = $divs.sort(function (a, b) {
        return $(a).find('.liked_votes').text() < $(b).find('.liked_votes').text();
    });
    $("#idea-list-group").html(thumbsUpOrderedDivs);
}

Related Questions