Created
March 30, 2019 11:14
-
-
Save jimmy89Li/10420fb2b87fbb782989c5e883ad85ed to your computer and use it in GitHub Desktop.
Sort objects by key
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
const list = [ | |
{ key: 7, val: "amazon" }, | |
{ key: 3, val: "msn" }, | |
{ key: 5, val: "github" }, | |
{ key: 1, val: "google" }, | |
{ key: 4, val: "stackoverflow" }, | |
{ key: 6, val: "jsfiddle" }, | |
{ key: 2, val: "yahoo" }, | |
{ key: 8, val: "ebay" } | |
]; | |
const sorted = list.sort(function(a, b){ | |
return a["key"] - b["key"]; | |
}); | |
console.log(sorted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment