Skip to content

Instantly share code, notes, and snippets.

@antoniocsoares
Last active April 27, 2017 18:20
Show Gist options
  • Save antoniocsoares/e509ec660733ff9f9821c1bb0d814efe to your computer and use it in GitHub Desktop.
Save antoniocsoares/e509ec660733ff9f9821c1bb0d814efe to your computer and use it in GitHub Desktop.
Ordering one object by values (Integer) - ES6
// example
const list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
Object
.keys(list)
.sort((a, b) => list[a]-list[b])
.reduce((obj, key) => ({
...obj,
[key]: list[key]
}), {})
// output
// {"bar": 15, "foo": 116, "me": 75, "you": 100}
// example without spread operator
const list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
Object
.keys(list)
.sort((a, b) => list[a]-list[b])
.reduce((obj, key) => Object.assign(obj, {[key]: list[key]}));
// output
// {"bar": 15, "foo": 116, "me": 75, "you": 100}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment