-4

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)
  • 1
    @JasonCust that's a better dupe target. I somehow didn't manage to find it. As for OP - the dupes should have all information you need. – VLAZ Oct 09 '18 at 18:39
  • @vlaz, the targets does not match the special problem to sort a specific item to top/bottom. – Nina Scholz Oct 09 '18 at 18:44
  • @vlaz I laughed a bit as it is basically the same "test" data set. ¯\_(ツ)_/¯ – Jason Cust Oct 09 '18 at 19:24

1 Answers1

-1

You could take a check if the key with the wanted value exist and sort this wanted value with a delta of the boolean check to top.

I simplified the comparison part with a default value.

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
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392