Skip to content

Instantly share code, notes, and snippets.

@EdwardGoomba
Created June 5, 2019 14:49
Show Gist options
  • Save EdwardGoomba/46ae827462802f9f3891dd68414172b3 to your computer and use it in GitHub Desktop.
Save EdwardGoomba/46ae827462802f9f3891dd68414172b3 to your computer and use it in GitHub Desktop.
Sorting an array of JavaScript objects by property
// 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