0248881
0248881

Reputation: 771

Sort Alphabetically and Numerically in jQuery

As you can see, it orders 95% of the divs alphabetically and numerically but its buggy. Not sure why it does that. If you press the buttons more than once you will see what I mean. Thank you!

It always seems to be the 1st, 2nd & 4th boxes.

var $divs = $("div.box");

$('#alphBnt').on('click', function() {
  var alphabeticallyOrderedDivs = $divs.sort(function(a, b) {
    return $(a).find("h1").text() > $(b).find("h1").text();
  });
  $("#container").html(alphabeticallyOrderedDivs);
});

$('#numBnt').on('click', function() {
  var numericallyOrderedDivs = $divs.sort(function(a, b) {
    return $(a).find("h2").text() > $(b).find("h2").text();
  });
  $("#container").html(numericallyOrderedDivs);
});
body {
  background: #eee;
  font-family: sans-serif;
}
.box {
  background: lightgreen;
  height: 200px;
  width: 200px;
  display: inline-block;
}
.box h1 {
  color: white;
  font-size: 1em;
  text-align: center;
}
.box h2 {
  color: black;
  font-size: 2.5em;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrap">
  <button id="alphBnt">Alphabetical</button>
  <button id="numBnt">Numerical</button>
  <div id="container">

    <div class="box">
  <h1>A</h1>
    <h2>7/10</h2>  
  </div>

  <div class="box">
    <h1>B</h1>
    <h2>8/10</h2>
  </div>

  <div class="box">
    <h1>C</h1>
    <h2>6.8/10</h2>  
  </div>

  <div class="box">
    <h1>D</h1>
    <h2>8/10</h2>
  </div>
  
  <div class="box">
  <h1>E</h1>
    <h2>510.25</h2>
  </div>
  
  <div class="box">
    <h1>F</h1>
    <h2>5,512</h2>
  
  <div class="box">
    <h1>G</h1>
    <h2>7/10</h2>  
  </div>

  <div class="box">
  <h1>H</h1>
    <h2>8/10</h2>
  </div>

  <div class="box">
    <h1>I</h1>
    <h2>6.8/10</h2>  
  </div>

  <div class="box">
    <h1>J</h1>
    <h2>8/10</h2>
  </div>
  
  <div class="box">
    <h1>K</h1>
    <h2>510.25</h2>
  </div>
  
  <div class="box">
    <h1>L</h1>
    <h2>5,512</h2>
  </div>

Upvotes: 5

Views: 1103

Answers (2)

Poul Kruijt
Poul Kruijt

Reputation: 71971

sort()'s callback function expects either negative, 0 or a positive value as a return value, because you return a boolean it will see false as a 0, which is evaluated as equal. If you try this sort function, you will see it works:

$('#alphBnt').on('click', function () {
    var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
        var at = $(a).find("h1").text();
        var bt =  $(b).find("h1").text();
        return at > bt ? 1 : (at < bt ? -1 : 0);
    });
    $("#container").html(alphabeticallyOrderedDivs);
});

Upvotes: 4

Anonymous Duck
Anonymous Duck

Reputation: 2998

I updated your script to correct sorting issues .

   var $divs = $("div.box");

    $('#alphBnt').on('click', function () {
        var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
            return case_insensitive_comp($(a).find("h1").text(), $(b).find("h1").text());
        });
        $("#container").html(alphabeticallyOrderedDivs);
    });

    $('#numBnt').on('click', function () {
        var numericallyOrderedDivs = $divs.sort(function (a, b) {
            return eval($(a).find("h2").text()) > eval($(b).find("h2").text());
        });
        $("#container").html(numericallyOrderedDivs);
    });

    function case_insensitive_comp(strA, strB) {
        return strA.toLowerCase().localeCompare(strB.toLowerCase());
    }

Upvotes: 1

Related Questions