我有这样的数组:
people = [
{id:1,name:'abc',age:18},
{id:2,name:'abcd',age:20},
{id:3,name:'abce',age:18},
{id:4,name:'abcf',age:18},
{id:5,name:'abcg',age:21},
{id:6,name:'abch',age:20},
{id:7,name:'abci',age:17},
]
现在,我想按JavaScript中的出现频率(年龄)对数组顺序进行排序。 输出如下:
people = [
{id:1,name:'abc',age:18},
{id:3,name:'abce',age:18},
{id:4,name:'abcf',age:18},
{id:2,name:'abcd',age:20},
{id:6,name:'abch',age:20},
{id:5,name:'abcg',age:21},
{id:7,name:'abci',age:17},
]
那我该怎么做?
我尝试按照Nina Scholz的回答进行操作,但是我遇到了这样的错误。
var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abcg', age: 21 }, { id: 8, name: 'abci', age: 17 }],
count = people.reduce((r, { age }) => (r[age] = (r[age] || 0) + 1, r), {});
people.sort((a, b) => count[b.age] - count[a.age]);
console.log(people);
输出为:
people = [
{id:1,name:'abc',age:18},
{id:3,name:'abce',age:18},
{id:4,name:'abcf',age:18},
{id:2,name:'abcd',age:20},
{id:5,name:'abcg',age:21},
{id:6,name:'abch',age:20},
{id:5,name:'abcg',age:21},
{id:7,name:'abci',age:17},
]
答案 0 :(得分:4)
只需计数年龄,然后按计数降序即可。
var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abci', age: 17 }],
count = people.reduce((r, { age }) => (r[age] = (r[age] || 0) + 1, r), {});
people.sort((a, b) => count[b.age] - count[a.age]);
console.log(people);
.as-console-wrapper { max-height: 100% !important; top: 0; }
要按count
和 age
进行排序,请添加logical OR ||
以及年龄段。
var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abcg', age: 21 }, { id: 8, name: 'abci', age: 17 }],
count = people.reduce((r, { age }) => (r[age] = (r[age] || 0) + 1, r), {});
people.sort((a, b) =>
count[b.age] - count[a.age] ||
b.age - a.age
);
console.log(people);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
尝试一下:
let people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abci', age: 17 }];
let tmpAge = {};
for (let i = 0; i < people.length; i++) {
if (!tmpAge[people[i].age]) {
tmpAge[people[i].age] = 1;
} else {
tmpAge[people[i].age]++;
}
}
people.sort((a, b) => tmpAge[b.age] - tmpAge[a.age]);
console.log(people);
答案 2 :(得分:0)
const people = [
{ id: 1, name: 'abc', age: 18 },
{ id: 2, name: 'abcd', age: 20 },
{ id: 3, name: 'abce', age: 18 },
{ id: 4, name: 'abcf', age: 18 },
{ id: 5, name: 'abcg', age: 21 },
{ id: 6, name: 'abch', age: 20 },
{ id: 7, name: 'abci', age: 17 },
];
const agesFrequency = people.reduce((previousValue, currentValue) => {
const { age } = currentValue;
return {
...previousValue,
[age]: (
previousValue[age] || 0
) + 1,
};
}, {});
people.sort((
(a, b) =>
agesFrequency[b.age] - agesFrequency[a.age]
));
console.log(people);
答案 3 :(得分:0)
我解决了。将功能排序更改为
java.util.Supplier
代表OP发布