Home > Enterprise >  JavaScript: Sort array of objects by computed property or by an existing property if equal
JavaScript: Sort array of objects by computed property or by an existing property if equal

Time:02-28

[
    { name: 'Joe', scores: [1, 2, 3] },
    { name: 'Jane', scores: [1, 2, 3] },
    { name: 'John', scores: [1, 2, 3] }
]

how do I make a function that sorts the elements first by the sum in scores and later by name?

CodePudding user response:

Using Array#sort, sort the array by the sum of scores, or name as fallback (using String#localeCompare)

const arr = [ { name: 'Joe', scores: [1] }, { name: 'Jane', scores: [1, 2] }, { name: 'John', scores: [1, 2] } ];

const sum = (arr = []) => arr.reduce((total, num) => total   num, 0);

const sorted = arr.sort((a, b) => 
  sum(b.scores) - sum(a.scores) || a.name.localeCompare(b.name)
);

console.log(sorted);

CodePudding user response:

You could take a Map for all sums and the object as key without mutating data.

const
    data = [{ name: 'Joe', scores: [1, 2, 3] }, { name: 'Jane', scores: [1, 4, 3] }, { name: 'John', scores: [1, 2, 1] }],
    add = (a, b) => a   b,
    sums = new Map(data.map(o => [o, o.scores.reduce(add, 0)]));

data.sort((a, b) => sums.get(b) - sums.get(a) || a.name.localeCompare(b.name));

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

CodePudding user response:

The lodash package could be handy

const lodash = require('lodash')

const list = [
  { name: 'Joe', scores: [1, 2, 4] },
  { name: 'Jane', scores: [1, 2, 3] },
  { name: 'John', scores: [1, 2, 4] }
]

const listWithSum = list.map(item => {
  return {
    ...item,
    sum: lodash.sum(item.scores)
  }
})

const sortedList = lodash.orderBy(listWithSum, ['score', 'name'], ['desc', 'desc'])

console.log(sortedList)
  • Related