1

I am trying to process a JSON object and do maths to calculate a weight. Order matters in my algorithm, but the order of products in the JSON object are passed to me in the order the customer adds them to their cart. I need to sort a number of attributes in the same fashion.

In the following example, a customer has added products in the "wrong" (1.5,1,1.25) order, so object.products looks as such:

"products":[
{
  "quantity": 3,
  "weight_each": 1.5
},
{
  "quantity": 4,
  "weight_each": 1
},
{
  "quantity": 2,
  "weight_each": 1.25
}

My first step is to add these objects to a more keyboard friendly array:

for (var i = 0; i < object.products.length; i++)
{
  quantity[i] = object.products[i].quantity;
  weight[i] = object.products[i].weight_each;
}

My algorithm, as previously mentioned, requires that products be ordered by weight (ascending). I know how to sort each array independently [weight.sort()], but I do not know how to sort each array so that, post-sort, weight[0] and quantity[0] reference the same product.

My end result should look like so:

console.log(weight)   // [1,1.25,1.5]
console.log(quantity) // [4,2,3]

I have google'd many different phrases and tried several different methods. I apologize if this has already been answered, or if this is a rudimentary question. I cannot seem to move past this roadblock on my own, and all answers are greatly appreciated! :)

EnergyWasRaw
  • 134
  • 8
  • possible duplicate of [Sorting a multidimensional array in javascript](http://stackoverflow.com/questions/2824145/sorting-a-multidimensional-array-in-javascript) – Pete Garafano Jan 12 '14 at 23:45
  • I don't know if this is significant, but the link you posted as a possible duplicate does not solve my problem, whereas MT0's answer does. – EnergyWasRaw Jan 12 '14 at 23:55

3 Answers3

1

Sort the products array on weight_each before separating the quantity and weight_each into two unconnected arrays:

input = '{"products":[{"quantity":3,"weight_each":1.5},{"quantity":4,"weight_each":1},{"quantity":2,"weight_each":1.25}]}';

data = JSON.parse( input );

data.products.sort( function(a,b){return a.weight_each - b.weight_each; } );

for ( var i = 0; i < data.products.length; i++)
{
  quantity[i] = data.products[i].quantity;
  weight[i]   = data.products[i].weight_each;
}
MT0
  • 143,790
  • 11
  • 59
  • 117
  • I'm going to give this a try, thank you for the quick response! I'll comment back after I'm finished with my stab at it! – EnergyWasRaw Jan 12 '14 at 23:39
  • I still don't fully understand how the sort function you supplied me with works, but I do know that it does work. Bonus points if you would be kind enough to explain in layman's terms. Regardless, thank you so much for your help! – EnergyWasRaw Jan 12 '14 at 23:49
  • `Array.sort()` can take a comparator function and when it tries to find whether one element of an array is before, equal or after another it will pass both objects to the comparator function and the return value will be -ve, 0 or +ve respectively. The default comparator for numeric values can be simplified to `function(a,b){return a-b;}` and will compare the value of the objects directly; however, providing a custom comparator `function(a,b){return a.x-b.x;}` will change it to comparing the values of the property `x` for both elements. – MT0 Jan 13 '14 at 00:00
  • See [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) for some more explanation and examples. – MT0 Jan 13 '14 at 00:03
  • It is more verbose but you could also rewrite the comparator function to be: `data.product.sort( function(a,b){ if(a.weight_eachb.weight_each)return +1; return 0;} )` – MT0 Jan 13 '14 at 00:07
  • Awesome! Thank you so much. That makes it quite clear. I would upvote you but I'm not allowed, less than 15 reputation... – EnergyWasRaw Jan 13 '14 at 00:46
0

Your first step is wrong! First sort the input array so that each product is sorted by weight. Then when you split it into two separate arrays they will be in the correct order.

Of course you can leave it like this but then you need to write manual sorting where you make the same actions to both arrays. For example if one sort step switches the places of index 0 and 1 then you do it on both arrays (and so on for all sroting steps).

Pavel Nikolov
  • 9,401
  • 5
  • 43
  • 55
-1

Upon adding an item to cart add a variable called total weight which is the quantity * weight. Then sort it by total weight. now matter how you spilt it, it will always be in order by weight.

robotmayo
  • 131
  • 10
  • In the example above this would give the sorted arrays as `total_weight=[2.5,4,4.5]`, `weight=[1.25,1,1.5]` and `quantity=[2,4,3]` which is not sorted into ascending order of either `weight` or `quantity`. How does this answer the question? – MT0 Jan 12 '14 at 23:39