1

Refering to earlier questions about referencing elements of and sorting a JSON (javascript) array. See refer to an element of JSON (Javascript) object Sorting an array of JavaScript objects

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);  
});
2
  • @BalusC, that's what I was wondering and why I was apprehensive about posting my answer. Commented Jan 18, 2010 at 12:29
  • en.wikipedia.org/wiki/Schwartzian_transform Commented Jan 18, 2010 at 12:36

4 Answers 4

5

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'));
Sign up to request clarification or add additional context in comments.

Comments

1

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

Sorting an array of JavaScript objects

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

Comments

0

I would recommend using a toolkit, for example jQuery. See Sorting JSON by values

3 Comments

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

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); 
}); 

3 Comments

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&A site, not a forum. As for the error, it's because $ isn't a standard object; it's defined by various libraries, which you probably didn't load.
I've added this updated information to your question - please delete this "answer".
Thanks for kind advice. JS library fell off the page during edit. I didn't even think of checking that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.