Last active
October 18, 2019 20:06
-
-
Save rootedsoftware/530db5dacb22f6ed84449ae8edd2f23d to your computer and use it in GitHub Desktop.
Sort an Array of Objects in JS
This file contains 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 users = [ | |
{ | |
firstName: 'Sarah', | |
lastName: 'Zulu', | |
}, | |
{ | |
firstName: 'Josh', | |
lastName: 'Able', | |
}, | |
]; | |
// Sort edits the array, if you don't want that you can clone it using slice | |
users.sort((a, b) => { | |
if(a.lastName < b.lastName) { return -1; } | |
if(a.lastName > b.lastName) { return 1; } | |
return 0; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment