'JavaScript: Sort array of objects by computed property or by an existing property if equal
[
{ 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?
Solution 1:[1]
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; }
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nina Scholz |
| Solution 2 | Krystian Sztadhaus |
