Created
August 16, 2019 14:20
-
-
Save mreis1/f16346aacc48fff38936940265f830b5 to your computer and use it in GitHub Desktop.
JS Snippets
This file contains 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
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