admin管理员组

文章数量:1025465

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

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

本文标签: javascriptsorting complex JSON objectStack Overflow