If I have a JavaScript object such as:
var list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
Is there a way to sort the properties based on value? So that I end up with
list = {
"bar": 15,
"me": 75,
"you": 100,
"foo": 116
};
Your objects can have any amount of properties and you can choose to sort by whatever object property you want, number or string, if you put the objects in an array. Consider this array:
sort by date born, oldest first
sort by name
http://jsfiddle.net/xsM5s/16/
Update with ES6: If your concern is having a sorted object to iterate through (which is why i'd imagine you want your object properties sorted), you can use the Map object.
You can insert your (key, value) pairs in sorted order and then doing a
for..of
loop will guarantee having them loop in the order you inserted themGithub Gist Link
many similar and useful functions: https://github.com/shimondoodkin/groupbyfunctions/
Thank you and continue answer @Nosredna
Now that we understand object need to be converted to array then sort the array. this is useful for sorting array (or converted object to array) by string: