1

I have a ajax function which actually pull this message_count data from server every 3 mins and update the attribute of each player accordingly... now i want to get the divs automatically gets sorted after this ajax call based on message_count attribute value in descending order... so the player who got more messages will come as first div.

var sortedDivs = jQuery(".players").find(".player").toArray().reverse(sorter);
jQuery.each(sortedDivs, function(index, value) {
  jQuery(".players").append(value);
});

function sorter(a, b) {
  return a.getAttribute('message_count') - b.getAttribute('message_count');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="players">
  <div class="player" message_count="8">8</div>
  <div class="player" message_count="2">2</div>
  <div class="player" message_count="0">0</div>
  <div class="player" message_count="1">1</div>
</div>

1
  • Use sort instead of reverse i.e. jQuery(".players").find(".player").sort(sorter).appendTo(".players"); function sorter(a, b) { return b.getAttribute('message_count') - a.getAttribute('message_count'); }; Commented Oct 23, 2017 at 12:01

3 Answers 3

5

Using jquery .sort will do it easily

var players = $(".players div");
var temp = players.sort(function(a,b){
  return parseInt($(b).attr("message_count")) - parseInt($(a).attr("message_count"));
});
$(".players").html(temp);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="players">
  <div class="player" message_count="8">Div 8</div>
  <div class="player" message_count="2">Div 2</div>
  <div class="player" message_count="0">Div 0</div>
  <div class="player" message_count="1">Div 1</div>
 </div>

Sign up to request clarification or add additional context in comments.

2 Comments

would this work for sorting by values that are iso date strings? eg new Date().toISOString() // 2018-09-22T05:51:53.731Z
can you add your question and share the link here ?
0
   $('div').sort(function (a, b) {

      var contentA =parseInt( $(a).attr('message_count'));
      var contentB =parseInt( $(b).attr('message_count'));
      return (contentA < contentB) ? -1 : (contentA > contentB) ? 1 : 0;
   })

3 Comments

numeric sort is simpler just doing valueA-valueB the same way OP is doing
Code only answers might solve the issue, but some explanation of how it solves the problem would help in learning to understand how to solve future problems.
What if we wanted to sort strings
0

You can use the javascript sort() method and just reverse the query.

.sort(function(a, b){return b.getAttribute('message_count') - a.getAttribute('message_count'))

var sortedDivs = jQuery(".players").find(".player").toArray().sort(function(a, b){return parseInt(b.getAttribute('message_count')) - parseInt(a.getAttribute('message_count'))});

jQuery.each(sortedDivs, function(index, value) {
  jQuery(".players").append(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="players">
  <div class="player" message_count="8">8</div>
  <div class="player" message_count="2">2</div>
  <div class="player" message_count="0">0</div>
  <div class="player" message_count="1">1</div>
</div>

Comments

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.