Skip to content

Instantly share code, notes, and snippets.

@rootedsoftware
Last active October 18, 2019 20:06
Show Gist options
  • Save rootedsoftware/530db5dacb22f6ed84449ae8edd2f23d to your computer and use it in GitHub Desktop.
Save rootedsoftware/530db5dacb22f6ed84449ae8edd2f23d to your computer and use it in GitHub Desktop.
Sort an Array of Objects in JS
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