Created
June 5, 2019 14:49
-
-
Save edwardoverthere/46ae827462802f9f3891dd68414172b3 to your computer and use it in GitHub Desktop.
Sorting an array of JavaScript objects by property
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
// json data example | |
// can be pulled from api or other source as needed | |
let homes = [ | |
{ | |
"h_id": "3", | |
"city": "Dallas", | |
"state": "TX", | |
"zip": "75201", | |
"price": "162500" | |
}, { | |
"h_id": "4", | |
"city": "Bevery Hills", | |
"state": "CA", | |
"zip": "90210", | |
"price": "319250" | |
}, { | |
"h_id": "5", | |
"city": "New York", | |
"state": "NY", | |
"zip": "00010", | |
"price": "962500" | |
} | |
]; | |
// create a function to sort the objects by the price property in ascending or descending order | |
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment