用javascript中的多个字段排序?

时间:2018-10-09 18:27:51

标签: javascript sorting

下面我有我的数据,我确实根据状态进行了排序,现在它像这样

state: ""
state: "CA"
state: "NY"
state: "TX"
state: "xz

但是我想检查区域字段,需要检查一下(34:"voterid") 如果我们在应该首先出现的任何对象中找到这个(34:"voterid"),则应该保持不变

我期望这样

state: "xz"
state: ""
state: "CA"
state: "NY"
state: "TX"

这是我的代码

var homes = [
    {  
   "h_id":"3",
   "city":"Dallas",
   "state":"TX",
   "zip":"75201",
   "price":"162500",
   "data":{  
      "d_id":"3",
      "varient":{  
         "sd":"ss",
         "area":{  
            4:"WARDCODE",
            5:"WARDData"
            
         }
      }
   }
},
{  
   "h_id":"4",
   "city":"Bevery Hills",
   "state":"CA",
   "zip":"90210",
   "price":"319250",
   "data":{  
      "d_id":"3",
      "varient":{  
         "sd":"ss",
         "area":{  
            2:"areacode",
            3:"villagecode"
         }
      }
   }
},
{  
   "h_id":"5",
   "city":"New York",
   "state":"NY",
   "zip":"00010",
   "price":"962500",
   "data":{  
      "d_id":"3",
      "varient":{  
         "sd":"ss",
         "area":{  
            
         }
      }
   }
},
{  
   "h_id":"6",
   "city":"xyz",
   "state":"",
   "zip":"000103",
   "price":"9622300",
   "data":{  
      "d_id":"4",
      "varient":{  
         "sd":"ss",
         "area":{  
            
         }
      }
   }
},
{  
   "h_id":"7",
   "city":"wer",
   "state":"xz",
   "zip":"003103",
   "price":"5622300",
   "data":{  
      "d_id":"5",
      "varient":{  
         "sd":"ss",
         "area":{  
            34:"voterid",
            56:"votercode"
         }
      }
   }
}
];

sortData('state');

function sortData(param) {
               
                var finaldata = function compare(a, b) {
          
                    var A = (a[param]) ? a[param] : "";
                    var B = (b[param]) ? b[param] : "";
                    if (A < B)
                        return -1;
                    if (A > B)
                        return 1;

                    return 0;

                };                

               homes.sort(finaldata);

            }
console.log(homes)

1 个答案:

答案 0 :(得分:-1)

您可以检查是否存在具有所需值的键,并使用布尔检查值的增量将所需值排序到顶部。

我用默认值简化了比较部分。

var homes = [{ h_id: "3", city: "Dallas", state: "TX", zip: "75201", price: "162500", data: { d_id: "3", varient: { sd: "ss", area: { "4": "WARDCODE", "5": "WARDData" } } } }, { h_id: "4", city: "Bevery Hills", state: "CA", zip: "90210", price: "319250", data: { d_id: "3", varient: { sd: "ss", area: { "2": "areacode", "3": "villagecode" } } } }, { h_id: "5", city: "New York", state: "NY", zip: "00010", price: "962500", data: { d_id: "3", varient: { sd: "ss", area: {} } } }, { h_id: "6", city: "xyz", state: "", zip: "000103", price: "9622300", data: { d_id: "4", varient: { sd: "ss", area: {} } } }, { h_id: "7", city: "wer", state: "xz", zip: "003103", price: "5622300", data: { d_id: "5", varient: { sd: "ss", area: { "34": "voterid", "56": "votercode" } } } }];

function sortData(param) {
    function compare(a, b) {
        return (b.data.varient.area[34] === 'voterid') - (a.data.varient.area[34] === 'voterid')
            || (a[param] || '').localeCompare(b[param] || '');
    }
    homes.sort(compare);
}

sortData('state');

console.log(homes.map(({ state }) => state)); // just to show states