Refering to earlier questions about referencing elements of and sorting a JSON (javascript) array. See http://stackoverflow.com/questions/2074908/refer-to-an-element-of-json-javascript-object http://stackoverflow.com/questions/979256/how-to-sort-a-json-array
Is it possible to sort one branch of a more complex javascript array, such as sorting by price in the example below?
var homes =
{
"Agents" : [
{
"name" : "Bob Barker"
},
{
"name" : "Mona Mayflower"
}
] ,
"Listings" : [
{
"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"
}
]
}
thanks you all your help!!!
EDIT
Sorry for the confusion. I meant Javascript as tag. (This should have been apparent by rest of question) I got the sort working, just having trouble iterating through the array.
// before sort
alert(homes.Listings[0].price);
// sort
homes.Listings.sort(sort_by('price', false, parseInt));
// after sort works:
alert(homes.Listings[0].price);
// iteration does not work "$ is not defined"
$.each(homes.Listings, function(i, thisHome) {
alert(thisHome.price);
});