Skip to content

Instantly share code, notes, and snippets.

@jimmy89Li
Created March 30, 2019 11:14
Show Gist options
  • Save jimmy89Li/10420fb2b87fbb782989c5e883ad85ed to your computer and use it in GitHub Desktop.
Save jimmy89Li/10420fb2b87fbb782989c5e883ad85ed to your computer and use it in GitHub Desktop.
Sort objects by key
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