dkon
dkon

Reputation: 85

Sorting an array of objects?

I have the following data structure and I'm stuck on a way to sorting all variants from all items in ascending order based on price.

Ideally I need a way to find the lowest price of any item variant while still having access to the variant's parent.

var items = [
{
    "id" : 1 ,
    "name" : "name1",
    "variations" : [
        {
            "subId" : 1000011,
            "subName" : "name1a",
            "price" : 19.99
        },
        {
            "subId" : 1000012,
            "subName" : "name1b",
            "price" : 53.21
        },
        {
            "subId" : 1000013,
            "subName" : "name1c",
            "price" : 9.49

        }
    ]

},
{
    "id" : 2, 
    "name" : "name2",
    "variations" : [
        {
            "subId" : 1000021,
            "subName" : "name2a",
            "price" : 99.29
        },
        {
            "subId" : 1000022,
            "subName" : "name2b",
            "price" : 104.99
        },
        {
            "subId" : 1000023,
            "subName" : "name2c",
            "price" : 38.00

        }
    ]

}

];

Upvotes: 2

Views: 71

Answers (3)

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

You have some syntax errors / mistypes in your .json. after fix you can use something like this:

var items = [
{
    "id" : 1 ,
    "name" : "name1",
    "variations" : [
        {
            "subId" : 1000011,
            "subName" : "name1a",
            "price" : 19.99
        },
        {
          "subId" : 1000012,
            "subName" : "name1b",
            "price" : 53.21
        },
        {
          "subId" : 1000013,
            "subName" : "name1c",
            "price" : 9.49

        }
    ]

},
{
    "id" : 2, 
    "name" : "name2",
    "variations" : [
        {
            "subId" : 1000021,
            "subName" : "name2a",
            "price" : 99.29
        },
        {
          "subId" : 1000022,
            "subName" : "name2b",
            "price" : 104.99
        },
        {
          "subId" : 1000023,
            "subName" : "name2c",
            "price" : 38.00

        }
    ]

}

];
items;

items.sort(function(a,b){
  var mina=a.variations.sort(function(x,y){
       return x.price - y.price;
     })[0].price; //find minimal price in 1st variations
  var minb=b.variations.sort(function(x,y){
       return x.price - y.price;
    })[0].price; // in 2nd variations
return mina - minb; //what compare returns 
});

Update 1

If you want more structural solution use this:

function minProp(arr, prop){
  return arr.sort(function(a,b){
    return a[prop] - b[prop];
  })[0][prop];
}

items.sort(function(a,b){
  var mina = minProp(a.variations,'price');
  var minb = minProp(b.variations,'price');
return mina-minb;  
});

More structure may be invoked.

Upvotes: 0

Josh Beam
Josh Beam

Reputation: 19772

Check out this jsbin example:

var sortedByPrice = items.map(function(item) {
  item.variations.sort(function(a, b) {
    return a.price - b.price;
  });

  return item;
});

console.log(sortedByprice);

Just map through them and sort based on the variation's price.

Upvotes: 0

Crisoforo Gaspar
Crisoforo Gaspar

Reputation: 3830

You can use the following code in order to do it:

items.forEach(function(item){
  var variations = item.variations.sort(function(a, b){
    return b.price - a.price;
  });
  console.log(variations);
});

This function is going to be called for every item inside of the items array. If you want to order only the first, or second or n item in the items array you need to use something like:

var variations = items[0].variations.sort(function(a, b){
  return b.price - a.price;
});
console.log(variations);

Where items[0] represents the item you want to order.

As you can see:

function(a, b){
  return b.price - a.price;
}

Is the function that orders the final result of the every array item from the items array. Using the sort function. You can read more about how the compare function works to sort the items of an array.

the array elements are sorted according to the return value of the compare function

Upvotes: 1

Related Questions