Skip to content

Instantly share code, notes, and snippets.

@mreis1
Created August 16, 2019 14:20
Show Gist options
  • Save mreis1/f16346aacc48fff38936940265f830b5 to your computer and use it in GitHub Desktop.
Save mreis1/f16346aacc48fff38936940265f830b5 to your computer and use it in GitHub Desktop.
JS Snippets
Sort Array of objects by property
var 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"
}
];
// Option1: Case sentitive
homes.sort((a,b)=>{
var string1 = a.city, string2 = b.city;
return (string1 > string2) - (string1 < string2)
})
// Option1: Case insensitive
homes.sort((a,b)=>{
var string1 = a.city, string2 = b.city;
return string1.localeCompare(string2)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment