Skip to content

Instantly share code, notes, and snippets.

@richleach
Created January 5, 2022 19:44
Show Gist options
  • Save richleach/5550f9c9a549317e3ba5cdc7a8528d7c to your computer and use it in GitHub Desktop.
Save richleach/5550f9c9a549317e3ba5cdc7a8528d7c to your computer and use it in GitHub Desktop.
//array of objects
const myItems = [{"name": "eggs", "price": 1},{"name": "coffee", "price": 9.99}, {"name": "rich", "price": 4.04}]
const sortByName = myItems.sort(function(a,b) {
let nameA = a.name.toUpperCase()
let nameB = b.name.toUpperCase()
if(nameA < nameB) {
return -1
}
if(nameA > nameB){
return 1
}
return 0
})
console.log(sortByName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment