我有一个如下数组:
var homes = [
{
"h_id": "3","city": "Dallas","state": "YYYY","zip": "75201","price": "162500"
},
{
"h_id": "4","city": "CA","state": "ZZZZ","zip": "90210","price": "319250"
},
{
"h_id": "5","city": "New York","state": "AAAA","zip": "00010","price": "962500"
},
{
"h_id": "5","city": "New York","state": "CCCC","zip": "00010","price": "962500"
}
];
我需要对其进行排序,这样我才能以升序和降序检查状态
结果需要如下:
var homes = [
{
"h_id": "5", "city": "New York", "state": "AAAA", "zip": "00010", "price": "962500"
},
{
"h_id": "5", "city": "New York", "state": "CCCC", "zip": "00010", "price": "962500"
},
{
"h_id": "3", "city": "Dallas", "state": "YYYY", "zip": "75201", "price": "162500"
},
{
"h_id": "4", "city": "CA", "state": "ZZZZ", "zip": "90210", "price": "319250"
},
];
答案 0 :(得分:2)
homes = homes .sort(function (a, b) {
return a.name.localeCompare(b.state);
});
您可以使用城市,h_id等代替州名