If I have a javascript object like this:
{ 'apples':14, 'oranges': 1, 'bananas':4 }
How can I sort it to an Array, descending order, based on the value?
Result:
[ 'apples', 'bananas', 'oranges' ]
Because 14, 4, 1 …descending order
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Pull all the keys from the object into an array and sort the array.
If you think you need to protect against properties that have been added to the Object prototype in an unfriendly way (or don’t know), then you should do it like this:
EDIT: now that the original question has been changed and the requirement is now that the keys are sorted in order by the value in the original object, that would have to be done differently: