Skip to content

Instantly share code, notes, and snippets.

@masautt
Last active September 3, 2019 22:58
Show Gist options
  • Select an option

  • Save masautt/17931ce2f5efee67ab58f8a8331e9c49 to your computer and use it in GitHub Desktop.

Select an option

Save masautt/17931ce2f5efee67ab58f8a8331e9c49 to your computer and use it in GitHub Desktop.
How to sort array of objects by value in JavaScript?
let arr = [
{ prop: "7" },
{ prop: "3" },
{ prop: "6" },
{ prop: "1" }
]
//Sort in ascending
arr.sort(function(a, b) {
return (a.prop - b.prop);
});
//Sort in descending
arr.sort(function(a, b) {
return (b.prop - a.prop);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment