0

This Function is used to sort divs depending on their like count

for some reason it works really fine except when it gets higher than 9

then suddenly 12 is < 8 for example ?

$(document).on("click", ".textbox", function() {
    let likes = parseInt($(this).closest(".textbox").find("span").text());
    console.log(likes)
    $(this).closest(".textbox").find("span").text(likes + 1);
  });


  $("#sort").on("click", function() {
    let divs = $(".boxwrapper")
    let sorted = divs.sort(function(a, b) {
       return $(a).find("span").text() < $(b).find("span").text() ? 1 : -1;
    });
    $(".container").append(sorted);

  });
  • 3
    Alphabetically (lexicographically), `"12" < "8"`. Just like you used `parseInt` on the text to get `+ 1` to mean addition instead of string concatenation, you have to parse the text to a number again when sorting to get numeric comparison instead of string comparison. – Ry- Jun 12 '20 at 19:40
  • 1
    Just change the line to return parseInt($(a).find("span").text()) < parseInt($(b).find("span").text()); instead of return $(a).find("span").text() < $(b).find("span").text(); – matthias_h Jun 12 '20 at 19:42
  • thanks you very much. i get no error message in the console but the sorting isnt working anymore. Oo – Tomerson Jefferson Jun 12 '20 at 20:17

0 Answers0