简体   繁体   中英

I want to sort an array of objects based on two properties

I want to sort an array of of objects in JavaScript with Two properties Boolean value and Int value.I want want Output like this :

{ first_nom: 'sumeet', last_nom: 'Prentice' ,offerApplicable: 'TRUE' ,price: 40},
4:55 PM { first_nom: 'Pirate', last_nom: 'Prentice' ,offerApplicable: 'TRUE' ,price: 50},
4:55 PM { first_nom: 'Lazslo', last_nom: 'Jamf'   ,offerApplicable: 'TRUE' ,price: 60 },
4:55 PM { first_nom: 'jitendra', last_nom: 'Prentice' ,offerApplicable: 'TRUE' ,price: 101}

All object with true values are first also sorted with price and then all object having boolean value as false. i am able to sort them by boolean value but i want to sort them by price as well.

i have tried this

var objs = [ 
    { first_nom: 'Lazslo', last_nom: 'Jamf'   ,offerApplicable: 'TRUE' ,price: 60 },
    { first_nom: 'Pig',    last_nom: 'Bodine'  , offerApplicable: 'FALSE' ,price: 100},
    { first_nom: 'Pirate', last_nom: 'Prentice' ,offerApplicable: 'TRUE' ,price: 50},
    { first_nom: 'nithesh',    last_nom: 'Bodine'  , offerApplicable: 'FALSE' ,price: 40},
    { first_nom: 'sumeet', last_nom: 'Prentice' ,offerApplicable: 'TRUE' ,price: 40},
    { first_nom: 'mahesh',    last_nom: 'Bodine'  , offerApplicable: 'FALSE' ,price: 40},
    { first_nom: 'jitendra', last_nom: 'Prentice' ,offerApplicable: 'TRUE' ,price: 101}
];
function compare(a,b) {
    var aConcat = a["offerApplicable"] + a["price"];
      var bConcat = b["offerApplicable"] + b["price"];
  if (aConcat < bConcat )
    return 1;
  if (aConcat > bConcat )
    return -1;
  return 0;

}`enter code here`

console.log(objs.sort(compare));

please help thanks in advance.

try this compare function

function compare(a,b) {
  if (a.offerApplicable == b.offerApplicable)
    return (a.price > b.price)
  else return (a.offerApplicable < b.offerApplicable)
}

What you need is to make three checks:

  1. First test if (a.offerApplicable == 'FALSE' && b.offerApplicable == 'TRUE') then return 1 .
  2. Then test if (a.offerApplicable == b.offerApplicable == 'TRUE') then return a.price > b.price .
  3. Otherwise test if (a.offerApplicable == b.offerApplicable) then return a.price > b.price .

This is how should be your code:

objs.sort(function compare(a,b) {
  if (a.offerApplicable == 'FALSE' && b.offerApplicable == 'TRUE')
    return 1;
  else if(a.offerApplicable == b.offerApplicable == 'TRUE')
     return a.price > b.price
  else if(a.offerApplicable == b.offerApplicable)
    return a.price > b.price
});

Demo:

 var objs = [{ first_nom: 'Lazslo', last_nom: 'Jamf', offerApplicable: 'TRUE', price: 60 }, { first_nom: 'Pig', last_nom: 'Bodine', offerApplicable: 'FALSE', price: 100 }, { first_nom: 'Pirate', last_nom: 'Prentice', offerApplicable: 'TRUE', price: 50 }, { first_nom: 'nithesh', last_nom: 'Bodine', offerApplicable: 'FALSE', price: 40 }, { first_nom: 'sumeet', last_nom: 'Prentice', offerApplicable: 'TRUE', price: 40 }, { first_nom: 'mahesh', last_nom: 'Bodine', offerApplicable: 'FALSE', price: 40 }, { first_nom: 'jitendra', last_nom: 'Prentice', offerApplicable: 'TRUE', price: 101 } ]; console.log(objs.sort(function compare(a,b) { if (a.offerApplicable == 'FALSE' && b.offerApplicable == 'TRUE') return 1; else if(a.offerApplicable == b.offerApplicable == 'TRUE') return a.price > b.price else if(a.offerApplicable == b.offerApplicable) return a.price > b.price })); 

This will sort upon offerApplicable then upon price .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM