1

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.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
WSM RWL
  • 47
  • 6
  • 1
    What exactly do you think the result of `'TRUE' + 60` is? – UnholySheep Jun 22 '17 at 11:48
  • @UnholySheep I think he understands it correctly (almost). He figures concatenating the price to the "offerApplicable" will let him sort by TRUE/FALSE first and then price second. Where he goes wrong is that while TRUE/FALSE will technically be "sorted" first, the remaining price gets sorted/compared as a string, not as a numeric value, e.g. `'60' > '400' === true`. – noahnu Jun 22 '17 at 11:54
  • 1
    Possible duplicate of [Sorting an Array of Objects by two Properties](https://stackoverflow.com/questions/10153846/sorting-an-array-of-objects-by-two-properties) – noahnu Jun 22 '17 at 11:56

2 Answers2

0

try this compare function

function compare(a,b) {
  if (a.offerApplicable == b.offerApplicable)
    return (a.price > b.price)
  else return (a.offerApplicable < b.offerApplicable)
}
Michal Toldy
  • 175
  • 7
0

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.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78