Last active
November 11, 2015 01:59
-
-
Save think2011/c07a86b0ccded21a789a to your computer and use it in GitHub Desktop.
给object排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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