-1

I need to sort this from highest to lowest and limit the number of records to 10 with highest values, I'm not good with programming and couldn't find similar examples. Any tips regarding code would also be appreciated<33

function getMostFrequent(arr) {
  const hashmap = arr.reduce( (acc, val) => {
   acc[val] = (acc[val] || 0 ) + 1
   return acc
},{})
return Object.entries(hashmap).sort((a, b) => b[1] - a[1]);
}

console.log(getMostFrequent(output));

this is the output I get

[
  [ '300', 173 ], [ '200', 31 ], [ '500', 31 ],
  [ '800', 29 ],  [ '700', 28 ], [ '600', 26 ],
  [ '400', 20 ],  [ '290', 11 ], [ '160', 10 ],
  [ '220', 10 ],  [ '250', 9 ],  [ '100', 8 ],
  [ '240', 8 ],   [ '260', 8 ],  [ '350', 8 ],
  [ '370', 8 ],   [ '230', 7 ],  [ '390', 7 ],
  [ '210', 6 ],   [ '280', 6 ],  [ '110', 5 ],
  [ '140', 5 ],   [ '150', 5 ],  [ '270', 5 ],
  [ '310', 5 ],   [ '320', 5 ],  [ '120', 4 ],
  [ '130', 4 ],   [ '330', 4 ],  [ '340', 4 ],
  [ '170', 3 ],   [ '190', 3 ],  [ '360', 2 ],
  [ '380', 2 ],   [ '', 1 ]
]
Reptilian
  • 1
  • 2
  • 1
    Can you please copy&paste the content of the hashmap as text in to the question so that we can use it to debug the code. Images of code are useless. – Rory McCrossan Oct 06 '22 at 16:02
  • It looks like you sorted it already. So you just need the first 10 entries? https://stackoverflow.com/questions/34883068/how-to-get-first-n-number-of-elements-from-an-array – James Oct 06 '22 at 16:04
  • DO NOT SORT HASHMAP VALUES! It screws up the lookup – ControlAltDel Oct 06 '22 at 16:05
  • If you want a sorted map, use a TreeMap – ControlAltDel Oct 06 '22 at 16:05
  • He's not sorting an object. He's sorting Object.entries(thatObject), which is an array. – James Oct 06 '22 at 16:06
  • @ControlAltDel I'm pretty sure the OP is mixing terminology, which is why I asked for the actual content to be added to the question. – Rory McCrossan Oct 06 '22 at 16:06
  • @James, I'm not sure, I'd like for the values to go from biggest to smallest but I'll look into the linked question so I can get the first 10 entries, thank you :)) – Reptilian Oct 07 '22 at 11:12
  • @RoryMcCrossan I might be mixing terminology, you're right ^^", I'll paste it as text when I get home, thank you :) – Reptilian Oct 07 '22 at 11:15

1 Answers1

0

@James was right, thank you so much<33 after implementing

var top10 = getMostFrequent(output).slice(0,10)

I got the correct answer, thanks everyone for your help :)!!

Reptilian
  • 1
  • 2