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);  
});
Community
  • 1
  • 1
rshid
  • 81
  • 1
  • 4
  • 6

4 Answers4

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'));
outis
  • 75,655
  • 22
  • 151
  • 221
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;
});
Community
  • 1
  • 1
Andy E
  • 338,112
  • 86
  • 474
  • 445
0

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

Community
  • 1
  • 1
Martin
  • 2,956
  • 7
  • 30
  • 59
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); 
}); 
rshid
  • 1
  • 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. – outis Jan 18 '10 at 14:37
  • I've added this updated information to your question - please delete this "answer". – Andy E Jan 18 '10 at 14:42
  • Thanks for kind advice. JS library fell off the page during edit. I didn't even think of checking that. – rshid Jan 18 '10 at 14:44