Search code examples
javascriptjquerymatrixcalculationjquery-calculation

Reordering divs by Matrix-Calculation (according to MULTIPLE data-attribute values)


I have a list of products that I wanna sort. Sorting by price and duration is an easy one. But I wanna find the BEST overall producte as well.

Here is what I have so far:

   var divList = $(".listing-item");

    /* -------------------------------------- */

    $("#btnPrice").click(function () {
       divList.sort(function(a, b) { 
         return $(a).data("price")-$(b).data("price")
       });
       $("#list").append(divList);
    });
    
    /* -------------------------------------- */

    $("#btnDuration").click(function () {
       divList.sort(function(a, b) { 
         return $(a).data("duration")-$(b).data("duration")
       });
       $("#list").append(divList);
    });

    /* -------------------------------------- */
    
    $("#btnBest").click(function () {
       /* TBD */
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="list">
    <div class="listing-item" data-price="20" data-duration="400" data-stops="3">Price 20, Duration 400, Stops 3</div>
    <div class="listing-item" data-price="30" data-duration="350" data-stops="3">Price 30, Duration 350, Stops 3</div>
    <div class="listing-item" data-price="10" data-duration="700" data-stops="2">Price 10, Duration 700, Stops 2</div>
    <div class="listing-item" data-price="40" data-duration="500" data-stops="1">Price 40, Duration 500, Stops 1</div>
</div>

    <button id="btnPrice" type="button">Sort by price</button>
    <button id="btnDuration" type="button">Sort by duration</button>
    <button id="btnBest" type="button">Sort by overall best</button>

The BEST overall product should have a cheap price, little duration and as less stops as possible.

I am thinking about kind of a "Matrix Calculation" here, comparing each factor of each product with the same attribute of all the other products. At the end we'll have a sum of all factor results.

e.g.

Product 1 has price of 20 and duration of 400 and stops = 3.
Product 2 has price of 30 and duration of 350 and stops = 3.

I would calculate as follows:

A

20/30 = 0.66
400/350 = 1.14
3/3 = 1
SUM = 1.8

B

30/20 = 1.5
350/400 = 0.875
3/3 = 1
SUM = 2.375

In this Calculation Product A wins, because the sum of all factors is less than the sum of all factors for product B.

So far for the theory. But how can I achive this with JS??


Solution

  • ok, I was able to solve it now.

    $("#btnBest").click(function () {
    
       var Price = [];
       var Duration = [];
       var Stops = [];
    
       divList.each(function() {
    
           Price.push($(this).data('price'));
           Duration.push($(this).data('duration'));
           Stops.push($(this).data('stops'));
    
       });
    
    
       for(i=0; i< divList.length; i++) {
    
           var PriceSum = 0;
           var DurationSum = 0;
           var StopsSum = 0;
           var OverallSum = 0;
    
           for(j=0; j < divList.length; j++) {
    
               var newPriceSum = Price[i] / Price[j];
               var newDurationSum = Duration[i] / Duration[j];
               var newStopsSum = Stops[i] / Stops[j];
    
               PriceSum = PriceSum + newPriceSum;
               DurationSum = DurationSum + newDurationSum;
               StopsSum = StopsSum + newStopsSum;
           }
    
           var OverallSum = PriceSum + DurationSum + StopsSum;
           $(".listing-item" ).eq( i ).attr("data-sum", OverallSum);
    
           //console.log(i + ": " + PriceSum + " + " + DurationSum + " + " + StopsSum + " = " + OverallSum);
    
        } 
    
        divList.sort(function(a, b) { 
            return $(a).data("sum")-$(b).data("sum")
        });
        $("#list").append(divList);
    
    });
    

    See working Fiddle, if interested. https://jsfiddle.net/SchweizerSchoggi/k3af7pb0/