Last active
August 30, 2016 16:39
-
-
Save auxcoder/a075e601a568dc91f19d82bc37568058 to your computer and use it in GitHub Desktop.
Order array of objects by object.attribute (sort)
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
| // function passed into sort to order by obj.key | |
| function compare(a,b) { | |
| if (a.sequence < b.sequence){ | |
| return -1; | |
| } | |
| if (a.sequence > b.sequence){ | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| var array = [ | |
| { | |
| sequence: 3, | |
| title: 'Lorem Ipsum', | |
| value: 125 | |
| }, | |
| { | |
| sequence: 0, | |
| title: 'Dolor at met', | |
| value: 25 | |
| }, | |
| { | |
| sequence: 1, | |
| title: 'Quantum Mobile', | |
| value: 15 | |
| } | |
| ]; | |
| console.log(array.sort(compare)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment