根据值对组进行排序
Sorting a group based on value
I have a group named p. On performing certain operations on this group, I get the corresponding output as shown-
p.all()
>> 01: {key: "A", value: {count:2}}
>> 102: {key: "B", value: {count:10}}
>> 103: {key: "C", value: {count:4}}
p5.all()[0].value.count
>> 2
p5.all()[1].value.count
>> 10
p5.all()[1].key
>> "B"
我想根据计数对前 2 个值进行排序。这样输出应该是:
{key: "B", value: {count:10}}
{key: "C", value: {count:4}}
如何排序?
const myArray = [
{key: "A", value: {count:2}},
{key: "B", value: {count:10}},
{key: "C", value: {count:4}},
]
const sortedArray = myArray.sort((a, b) => b.value.count - a.value.count);
console.log(sortedArray);
const groupP = [
{key: "A", value: {count:2}},
{key: "B", value: {count:10}},
{key: "C", value: {count:4}},
]
const sortedGroupP = groupP.sort((a, b) => a.value.count - b.value.count);
console.log(sortedGroupP[groupP.length -2]);
console.log(sortedGroupP[groupP.length - 1]);
const myArray = [
{ key: "A", value: { count: 2 } },
{ key: "B", value: { count: 10 } },
{ key: "C", value: { count: 4 } },
];
const result = myArray.sort((a, b) => b.value.count - a.value.count).splice(0, 2);
console.log(result);
I have a group named p. On performing certain operations on this group, I get the corresponding output as shown-
p.all()
>> 01: {key: "A", value: {count:2}}
>> 102: {key: "B", value: {count:10}}
>> 103: {key: "C", value: {count:4}}
p5.all()[0].value.count
>> 2
p5.all()[1].value.count
>> 10
p5.all()[1].key
>> "B"
我想根据计数对前 2 个值进行排序。这样输出应该是:{key: "B", value: {count:10}}
{key: "C", value: {count:4}}
如何排序?
const myArray = [
{key: "A", value: {count:2}},
{key: "B", value: {count:10}},
{key: "C", value: {count:4}},
]
const sortedArray = myArray.sort((a, b) => b.value.count - a.value.count);
console.log(sortedArray);
const groupP = [
{key: "A", value: {count:2}},
{key: "B", value: {count:10}},
{key: "C", value: {count:4}},
]
const sortedGroupP = groupP.sort((a, b) => a.value.count - b.value.count);
console.log(sortedGroupP[groupP.length -2]);
console.log(sortedGroupP[groupP.length - 1]);
const myArray = [
{ key: "A", value: { count: 2 } },
{ key: "B", value: { count: 10 } },
{ key: "C", value: { count: 4 } },
];
const result = myArray.sort((a, b) => b.value.count - a.value.count).splice(0, 2);
console.log(result);