I'm trying to sort a list of divs from several factors, I've changed the code to be more simple, yet I still don't get the desired result.
I want to be able to sort my products on several "scores" and also name. I've only been able to make it sort by one score. I would like to sort the divs by the two different data sets.
Can anyone see what I'm doing wrong?
jQuery:
$(document).ready(function () {
$(".product-box").show();
$('#sortScore').click(function () {
var sortVar = "score";
var divList = $(".product-box");
divList.sort(function (a, b) {
return $(b).data(sortVar) - $(a).data(sortVar)
});
$(".wrapper").html(divList);
});
$('#sortName').click(function () {
var sortVar = "name";
var divList = $(".product-box");
divList.sort(function (a, b) {
return $(b).data(sortVar) - $(a).data(sortVar)
});
$(".wrapper").html(divList);
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='sortScore'>Score</div>
<div id='sortName'>Name</div>
<div class='wrapper'>
<div class='product-box' style='height:100px; width:100px; float:left;' data-name='Product 1' data-score='1.26'>
<p class='productInfo' style='float:left;'>
<strong>Product 1</strong>
<br />Score: 1.26</p>
</div>
<div class='product-box' style='height:100px; width:100px; float:left;' data-name='Another Product' data-score='5.43'>
<p class='productInfo' style='float:left;'>
<strong>Another Product</strong>
<br />Score: 5.43</p>
</div>
<div class='product-box' style='height:100px; width:100px; float:left;' data-name='Serious Product' data-score='-1.87'>
<p class='productInfo' style='float:left;'>
<strong>Serious Product</strong>
<br />Score: -1.87</p>
</div>
<div class='product-box' style='height:100px; width:100px; float:left;' data-name='Is this your product?' data-score='3.33'>
<p class='productInfo' style='float:left;'>
<strong>Is this your product?</strong>
<br />Score: 3.33</p>
</div>
<div class='product-box' style='height:100px; width:100px; float:left;' data-name='A Product' data-score='0.25'>
<p class='productInfo' style='float:left;'>
<strong>A Product</strong>
<br />0.25</p>
</div>