Below i have my data and i did sort based on state now it is coming like this
state: ""
state: "CA"
state: "NY"
state: "TX"
state: "xz
but i want to check area field and need to check this(34:"voterid"
)
if we find this (34:"voterid"
) in any object that should be come first remaing should be come as it is
i am expecting like this
state: "xz"
state: ""
state: "CA"
state: "NY"
state: "TX"
here is my code
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)