-1

I'm having trouble sorting through this array in a way that's in the title where words are the keys and value is the count.

const words = {
  "be": 3,
  "a": 5,
  "an": 3
}

Desired out put would be:

{ 
"a": 5,
"an": 3, 
"be": 3
}

Object.entries(wordCount).sort(([, a], [, b]) => b - a)

But I don't know how I would sort items with the same value in abc order :'(

Ghost
  • 17
  • 4
  • Does this answer your question? [Sorting object property by values](https://stackoverflow.com/questions/1069666/sorting-object-property-by-values) – jabaa Dec 01 '22 at 16:35
  • Does this answer your question? [Javascript - sorting array by multiple criteria](https://stackoverflow.com/questions/28560801/javascript-sorting-array-by-multiple-criteria) – jabaa Dec 01 '22 at 16:36
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – jabaa Dec 01 '22 at 16:36

1 Answers1

-1

Using the clever technique found in another answer:

const words = {
  "be": 3,
  "a": 5,
  "an": 3
}

const sortedEntries = (o) => 
    Object.entries(words)
          .sort(([k1, v1], [k2, v2]) => 
              v2 - v1 || k1.localeCompare(k2))

console.log(sortedEntries(words))
Ben Aston
  • 53,718
  • 65
  • 205
  • 331