-2

I have a dictionary

var unsorted = { "Bob":500, "James": 200, "Alex":750 }

I want an to create a list that would be like this:

var sorted = [["Alex",750],["Bob",500],["James",200]]

The sorted list is in order from high to low based on the value of the dictionary.

Dexygen
  • 12,287
  • 13
  • 80
  • 147
Ayush Parikh
  • 85
  • 1
  • 6
  • 1
    https://stackoverflow.com/questions/1069666/sorting-object-property-by-values – Tinu Jos K Jan 16 '20 at 13:27
  • 1
    That's a Javascript object, not a "dictionary". While it might be called that in other languages, you need to learn to use Javascript nomenclature in order to effectively communicate with other Javascript developers – Dexygen Jan 16 '20 at 13:29
  • @Dexygen I've been a JS developer for a long time, and given the fact that almost everything in JS is an Object (e.g. [] instanceof Object, Function.prototype instanceof Object, etc), it's quite common to refer to Objects that are used as dictionaries as a "Dictionary". Although, it is true that since ES6 we have the [Map](https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/Map) data-structure which is roughly the equivalent to what Dictionaries are in other languages :-) – Josep Jan 16 '20 at 13:47
  • @Josep No it is not *quite* common, only among C#/Python developers maybe, and they are wrong to do so. Besides possibly confusing Javascript devevlopers without the background in those languages, it also can lead to the incorrect "Dictonary" keys being applied, instead of "Javascript-Objects". – Dexygen Jan 16 '20 at 13:52
  • "Dictionary" tag I meant – Dexygen Jan 16 '20 at 15:47

2 Answers2

1

I would do it like this:

const unsorted = { "Bob": 500, "James": 200, "Alex": 750 };

const sorted = Object.entries(unsorted) // object to array of arrays
                     .sort((a, b) => b[1] - a[1]); // sort descending by 2nd element of the arrays

console.log(sorted);
LukStorms
  • 28,916
  • 5
  • 31
  • 45
Josep
  • 12,926
  • 2
  • 42
  • 45
  • Just FYI, its a bad practice to answer a question that does not show effort – Rajesh Jan 16 '20 at 13:29
  • 1
    Hi @Rajesh! Thanks for letting me know that, I haven't been very active on stackoverflow lately. Do you have a reference of that? Or is that just based on your personal opinion? I mean, could you please provide a reference or something? – Josep Jan 16 '20 at 13:31
  • Then why answer instead of down-voting? Also it's a duplicate – Dexygen Jan 16 '20 at 13:35
  • 3
    Hi @Dexygen, it is not exactly a duplicate... the OP wants the output to be an Array of tuples, not a plain object. It's quite similar, but I don't think that it is exactly a duplicate. Also, I don't think that it is very nice to down-vote a new-contributor for something like this. – Josep Jan 16 '20 at 13:40
  • Multiple answers on the duplicate are about sorting entries including the accepted answer. [This one](https://stackoverflow.com/a/37607084/3082296) uses `Object.entries()`. (But, please don't downvote correct answers) – adiga Jan 16 '20 at 13:46
  • 1
    @Josep Please refer: https://meta.stackoverflow.com/questions/270989/should-i-answer-good-questions-for-which-the-op-has-not-tried-anything-no-effor, https://meta.stackoverflow.com/questions/274630/should-we-add-a-do-my-work-for-me-close-reason and https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. The whole point is to not spoon-feed a user. – Rajesh Jan 16 '20 at 13:57
  • 1
    @Josef I don't think there's a written rule for having to explain your code ;) It's just something that comes natural to the other viewers who look at answers. If there's just code without a bit of explaination then they are less likely to upvote it. But i.m.h.o. that shouldn't deserve a downvote when the code works. Well, in the case of javascript, putting it in a snippet with inline comments often helps. – LukStorms Jan 16 '20 at 14:10
1

may be this one ?

const unsorted = { "Bob":500, "James": 200, "Alex":750 }


const sorted = Object.keys(unsorted)
                .sort((a,b)=>unsorted[b]-unsorted[a])
                .map(e=>[e,unsorted[e]])

console.log( JSON.stringify(sorted) ) // [["Alex",750],["Bob",500],["James",200]]
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40