Skip to content

Instantly share code, notes, and snippets.

@think2011
Last active November 11, 2015 01:59
Show Gist options
  • Save think2011/c07a86b0ccded21a789a to your computer and use it in GitHub Desktop.
Save think2011/c07a86b0ccded21a789a to your computer and use it in GitHub Desktop.
给object排序
// by value
var list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
var keysSorted = Object.keys(list).sort(function (a, b) {
return list[a] - list[b]
});
console.log(keysSorted);
// by key
var list = {100: "you", 75: "me", 116: "foo", 15: "bar"};
var keysSorted = Object.keys(list)
.sort(function (a, b) {
return a - b;
})
.map(function (v) {
return list[v]
});
console.log(keysSorted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment