最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - sorting complex JSON object - Stack Overflow

matteradmin5PV0评论

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

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 plex 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);  
});
Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Jan 18, 2010 at 12:17 rshidrshid 811 gold badge4 silver badges6 bronze badges 2
  • @BalusC, that's what I was wondering and why I was apprehensive about posting my answer. – Andy E Commented Jan 18, 2010 at 12:29
  • en.wikipedia/wiki/Schwartzian_transform – Dyno Fu Commented Jan 18, 2010 at 12:36
Add a ment  | 

4 Answers 4

Reset to default 5

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

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

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

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

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); 
}); 
Post a comment

comment list (0)

  1. No comments so far