json数组字符值升序和降序打字稿

时间:2019-05-24 12:51:03

标签: arrays json angular ionic-framework ionic4

我有一个如下数组:

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" 
 },
];

1 个答案:

答案 0 :(得分:2)

homes = homes .sort(function (a, b) {
    return a.name.localeCompare(b.state);
});

您可以使用城市,h_id等代替州名