Search code examples
javascripthighchartsmapreducecrossfilter

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"
I want to sort the top 2 values based on the count. So that the output should be:
{key: "B", value: {count:10}}
{key: "C", value: {count:4}}

How can I sort it as such?


Solution

  • 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);