0

I would like to know how to sort object containing nested array in javascript. I have obj in which each property value (color, product) needs to be sorted by label

var obj = {
 color: [{id:1, label: "white"}, {id:2, label: "black"}, {id:3, label:"purple"}],
 product: [{id: 1, label: "women"},{id: 4, label: "men"}, {id: 3,label:"kids"}]
}

function sortobj(obj){
  var result = obj.forEach(grp => {
    return grp.sort((a, b) => a.label- b.label)
  })
  return result;
}

var result = sortobj(obj);

Expected Output

{
  color: [{id:2, label: "black"}, {id:3, label:"purple"}, {id:1, label: "white"}],
  product: [{id: 3,label:"kids"}, {id: 4, label: "men"}, {id: 1, label: "women"}]
}
Fraction
  • 11,668
  • 5
  • 28
  • 48
miyavv
  • 763
  • 3
  • 12
  • 30

2 Answers2

0

First call Object.entries() to get an array of keys/values from the object then use Array.reduce() and String.localeCompare() as follows:

var obj = {
 color: [{id:1, label: "white"}, {id:2, label: "black"}, {id:3, label:"purple"}],
 product: [{id: 1, label: "women"},{id: 4, label: "men"}, {id: 3,label:"kids"}]
};

function sortObj(obj){
  return Object.entries(obj) // [['color', [{...}, {...}, ...]], ['product', [...]]]
    .reduce((acc, [key, value]) => 
    ({ ...acc, [key]: value.sort((a, b) => a.label.localeCompare(b.label)) }), {});
    // or
    //(acc[key] = value.sort((a, b) => a.label.localeCompare(b.label)), acc), {});
}

var result = sortObj(obj);

console.log(result);

//Expected Output
//  {
//  color: [{id:2, label: "black"}, {id:3, label:"purple"},{id:1, label: "white"}],
//  product: [{id: 3,label:"kids"},{id: 4, label: "men"},{id: 1, label: "women"}]
//  }
Fraction
  • 11,668
  • 5
  • 28
  • 48
  • Why do you need to re-create the object in order to just sort the values of its properties? – VLAZ May 25 '20 at 15:20
  • it depends on the context, but we can also add new properties to the same object – Fraction May 25 '20 at 15:36
  • We can do many things the question doesn't ask for. But this is very roundabout even if we chose to extend the object. There is no reason to either clone the object nor add any new properties. The original object also gets its array properties sorted in the meantime, so it seems wasteful and useless to create the clone. – VLAZ May 25 '20 at 15:38
  • so it's even useless to create the sort function and assign the result to another variable and we can just use the original object but that's what the OP asked for (if you look at the code provided) – Fraction May 25 '20 at 16:09
-1

You will need to iterate over the keys (properties) of your original object, grab the value of every property and sort that by label.

function sortobj(obj) {
  return Object.keys(obj).reduce((sortedObj, key) => {
    const value = obj[key];
    const sortedValue = value.sort((a, b) => a.label.localeCompare(b.label));

    return {
      ...sortedObj,
      [key]: sortedValue
    };
  }, {});
}
Ján Jakub Naništa
  • 1,880
  • 11
  • 12