views:

362

answers:

5

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);  
});
+4  A: 

The standard Array.sort takes a comparator function. Use that:

function makeNumericCmp(property) {
    return function (a, b) {
        return parseInt(a[property]) - parseInt(b[property]);
    };
}
homes.Listings.sort(makeNumericCmp('price'));
outis
+1  A: 

The answer is more-or-less in the question you posted a link to:

http://stackoverflow.com/questions/979256/how-to-sort-a-json-array

homes.Listings.sort(function (a, b)
{
    return a.price - b.price;
});
Andy E
A: 

I would recommend using a toolkit, for example jQuery. See http://stackoverflow.com/questions/881510/json-sorting-question

Martin
Why the down vote? Explaining your down vote seems like a decent thing to do.
Martin
I did not vote you down, but I suggest you are recommend a big hammer for a small problem.
Upper Stage
You're right, I didn't know about "The standard Array.sort takes a comparator function."
Martin
A: 

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); 
}); 
rshid
This post is fine if intended as an answer to your question (which you're allowed to do), but otherwise you should edit your question to add clarifications. SO is a Q it's defined by various libraries, which you probably didn't load.
outis
I've added this updated information to your question - please delete this "answer".
Andy E
Thanks for kind advice. JS library fell off the page during edit.I didn't even think of checking that.
rshid