I need to sort a list of divs by two values. In my example, the div with the shortest duration should be on top of the list. So far I got it working. BUT if I have two divs with the same duration, the sort function should have the one on top that has the shortest duration AND the lowest price.
How do I combine two of this sort functions?
The result should be:
Duration: 500 - Price: 190
Duration: 500 - Price: 250
Duration: 600 - Price: 100
Duration: 700 - Price: 130
var List__fastest = "";
List__fastest = $("div");
List__fastest.sort(function (a, b) { return $(a).data("duration") - $(b).data("duration") });
$("#result").html(List__fastest);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div data-duration="700" data-price="130">Duration: 700 - Price: 130</div>
<div data-duration="500" data-price="250">Duration: 500 - Price: 250</div>
<div data-duration="600" data-price="100">Duration: 600 - Price: 100</div>
<div data-duration="500" data-price="190">Duration: 500 - Price: 190</div>
<div id="result"></div>
List__fastest.sort(function (a, b) { return $(a).data("duration") - $(b).data("duration") || $(a).data("price") - $(b).data("price") });