Created
January 5, 2022 19:44
-
-
Save richleach/5550f9c9a549317e3ba5cdc7a8528d7c to your computer and use it in GitHub Desktop.
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
//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