0

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);
}
4
  • Your sorting function is not symmetric Commented Feb 7, 2019 at 13:28
  • Do the curlys {{ }} indicate Angular? In which case, or for a similar framework, I would look into a sort or orderBy pipe to do the sorting. Commented Feb 7, 2019 at 13:35
  • @AndyG it's django Commented Feb 7, 2019 at 14:04
  • Possible duplicate of Javascript : natural sort of alphanumerical strings Commented Feb 7, 2019 at 14:12

2 Answers 2

2

You should use String.prototype.localeCompare()

localCompare:The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order

if(this.name == 'thumbsUp'){
    var thumbsUpOrderedDivs = $divs.sort(function (a, b) {
        return ($(a).find('.liked_votes').text() + '').localeCompare($(b).find('.liked_votes').text());
    });
    $("#idea-list-group").html(thumbsUpOrderedDivs);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I got localCompare is not a function, I'm trying solve it.
It's supposed to be localeCompare. I have made an edit
take care with localCompare, in some browsers is not compatible developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@ÁlvaroTouzón not compatible with expanded usage, the suggested here is fine.
1

try parsing the text to numbers

    var thumbsUpOrderedDivs = $(".liked_votes").sort(function (a, b) {
        return  parseInt($(a).text()) - parseInt($(b).text());
    });
    
    $(".badge").html(thumbsUpOrderedDivs);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="like badge">
        <span class="liked_item_{{item.id}} liked_votes">
         5
        </span>
          <span class="liked_item_{{item.id}} liked_votes">
         1
        </span>
        
          <span class="liked_item_{{item.id}} liked_votes">
         3
        </span>
        
          <span class="liked_item_{{item.id}} liked_votes">
         100
        </span>
      </a>

1 Comment

That is still not a symmetric sorting function and would not guarantee correct sorting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.