LowCool
LowCool

Reputation: 1411

how to sort data based on nested attribute value n javascript

I have array like this

   [{key:'A',map:[{
volume:2000,
year:2017
},{
volume:2000,
year:2018
}]
},
{key:'B',map:[{
volume:2000,
year:2017
},{
volume:1000,
year:2018
}]
},
{key:'C',map:[{
volume:2000,
year:2017
},{
volume:3000,
year:2018
}]
}
]

now I have to sort it based on the volume or particular year i.e if I am doing sorting on 2018 year volume my result should be like this

[{key:'C',map:[{
volume:2000,
year:2017
},{
volume:3000,
year:2018
}]
},
{key:'A',map:[{
volume:2000,
year:2017
},{
volume:2000,
year:2018
}]
},
{key:'B',map:[{
volume:2000,
year:2017
},{
volume:1000,
year:2018
}]
}
]

I tried with java script normal sort but it didn't help please anyone let me know how to do that?

EDIT 1: here is my sort function

 sortedData = currentYearData.sort(
            function(a,b){
             let aData = a.map(data => data.map).filter(data => data.year  ===  2018);
             let bData = b.map(data => data.map).filter(data => data.year  ===  2018);
             if(aData.volume < bData.volume)
              return 1;
             if(bData.volume < aData.volume)
              return -1;
             else 
              return 0
            }
          );

Upvotes: 0

Views: 58

Answers (2)

Walk
Walk

Reputation: 757

Here it is:

var ar = [{
    key: 'A',
    map: [{
      volume: 2000,
      year: 2017
    }, {
      volume: 2000,
      year: 2018
    }]
  },
  {
    key: 'B',
    map: [{
      volume: 2000,
      year: 2017
    }, {
      volume: 1000,
      year: 2018
    }]
  },
  {
    key: 'C',
    map: [{
      volume: 2000,
      year: 2017
    }, {
      volume: 3000,
      year: 2018
    }]
  }
];

function compare2018Only(a, b) {
  valA = a.map.filter(obj => obj.year == 2018)[0];
  valB = b.map.filter(obj => obj.year == 2018)[0];
  return valB.volume - valA.volume;
}

var result = ar.sort(compare2018Only);
console.log(result);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386550

You could filter with a seach for the value of the specified year and then sort descending.

var data = [{ key: 'A', map: [{ volume: 2000, year: 2017 }, { volume: 2000, year: 2018 }] }, { key: 'B', map: [{ volume: 2000, year: 2017 }, { volume: 1000, year: 2018 }] }, { key: 'C', map: [{ volume: 2000, year: 2017 }, { volume: 3000, year: 2018 }] }];

data.sort(function (a, b) {
    function getValue(array) {
        var value;
        array.some(function (a) {
            if (a.year === 2018) {
                value = a.volume;
                return true;
            }
        });
        return value;
    }
    return getValue(b.map) - getValue(a.map);
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6

var data = [{ key: 'A', map: [{ volume: 2000, year: 2017 }, { volume: 2000, year: 2018 }] }, { key: 'B', map: [{ volume: 2000, year: 2017 }, { volume: 1000, year: 2018 }] }, { key: 'C', map: [{ volume: 2000, year: 2017 }, { volume: 3000, year: 2018 }] }],
    fn = o => o.year === 2018;

data.sort((a, b) => b.map.find(fn).volume - a.map.find(fn).volume);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions