title: How can I sort and get sorted data by data-weight attribute? Jquery
body: <p><a href="https://i.stack.imgur.com/pJCAO.png" rel="nofollow noreferrer">1</a>[after using a code from @Justinas]    </p>
  
  <pre><code>&lt;div&gt;
          &lt;span class="r1 c1 d1" data-weight="3"&gt;&lt;/span&gt;
          &lt;span class="r1 c2" data-weight="2"&gt;&lt;/span&gt;
          &lt;span class="r1 c3 d2" data-weight="3"&gt;&lt;/span&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;span class="r2 c1" data-weight="2"&gt;&lt;/span&gt;
          &lt;span class="r2 c2 d1 d2" data-weight="4"&gt;&lt;/span&gt;
          &lt;span class="r2 c3" data-weight="2"&gt;&lt;/span&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;span class="r3 c1 d2" data-weight="3"&gt;&lt;/span&gt;
          &lt;span class="r3 c2" data-weight="2"&gt;&lt;/span&gt;
          &lt;span class="r3 c3 d1" data-weight="3"&gt;&lt;/span&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>So, those are spans and divs i got, I'm making a Tic Tac Toe game (computer vs player) and it should work like i cant beat computer. I put data-weight attributes to show how much worth is exact span. I have problems with attack function- I need to make it like it attacks player at first in most valuable spans (middle row and column, then diagonals, and then all other /more details in picture added <a href="https://i.stack.imgur.com/DxX50.png" rel="nofollow noreferrer">2</a> )
  I really don't know how to make a function to get all these data-weights and actually sort them in descending order to choose the most valuable span of all available... Thank You! </p>
  
  <p>I made before a function that just attacked in every span in order of appearance not by data-weight.</p>
  
  <pre><code>    function attack() {
              var span = ['.r1 c1 d1', '.r2 c1', '.r3 c1 d2', '.r1 c2', '.r2 c2 d1 d2', '.r3 c2', '.r1 c3 d2', '.r2 c3', '.r3 c3 d1'];
              var weight = $('[data-weight]');
              var tempLoop = true;
              $.each(weight, function (k, span){
                  var weight= $(weight).data('weight');
                  $(span).each(function () {
                      if (tempLoop) {
                          if (isNaN($(this).data("full"))) {
                              $(this).data("full", 1)
                                      .data("value", -1)
                                      .html("&lt;i class=\"fa fa-times\"&gt;&lt;/i&gt;");
                              console.log("attack");
                              tempLoop = false;
                          }
                      }
                  });
              })
          }
  </code></pre>