Skip to content

Instantly share code, notes, and snippets.

@kevin-miles
Created November 19, 2014 16:29
Show Gist options
  • Save kevin-miles/375a485ab6909e9039a7 to your computer and use it in GitHub Desktop.
Save kevin-miles/375a485ab6909e9039a7 to your computer and use it in GitHub Desktop.
Sort array of javascript objects
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"
}];
//ascending
homes.sort(function(a,b) { return parseFloat(a.price) - parseFloat(b.price) } );
//swap a.price and b.price to sort descending:
//homes.sort(function(a,b) { return parseFloat(b.price) - parseFloat(a.price) } );
//apparently this can be used for strings
// if ( a.region < b.region )
// return -1;
// if ( a.region > b.region )
// return 1;
// return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment