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
4 Answers
Reset to default 5The 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
4 Answers
Reset to default 5The 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
版权声明:本文标题:javascript - sorting complex JSON object - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629603a2160079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论