Last active
September 3, 2019 22:58
-
-
Save masautt/17931ce2f5efee67ab58f8a8331e9c49 to your computer and use it in GitHub Desktop.
How to sort array of objects by value in JavaScript?
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
| 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