sort items by two different properties javascript

Viewed 63

I have an array of objects.

const people = [
   {
     active: true,
     hasMoney: false
   },
   {
     active: false,
     hasMoney: false
   },
   {
     active: true,
     hasMoney: true
   }
]

I want to sort the data in this order: active: true -> hasMoney: true -> active: false -> hasMoney: false

Important: If the user is active === true and hasMoney === true, then this should be ordered after active === true and hasMoney === false

I tried the following but it didn't work. Anyone have any ideas?

people
  .sort((x, y) =>
     x.hasMoney
       ? Number(x) - Number(y)
       : Number(isPersonActive(y)) - Number(isPersonActive(x))
)
5 Answers

You could take the delta of the boolean values and sort by two properties with a special look to the sorting of same values.

const people = [{ active: false, hasMoney: true }, { active: true, hasMoney: false }, { active: false, hasMoney: false }, { active: true, hasMoney: true }];

people.sort((a, b) =>
    b.active - a.active || 
    (a.hasMoney === b.active) - (a.active === b.hasMoney)
);

console.log(people);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can do the following,

const people = [
   {
     active: true,
     hasMoney: true
   },
   {
     active: false,
     hasMoney: false
   },
   {
     active: true,
     hasMoney: false
   }
]

people.sort((a, b) => {
  if(a.active && b.active) {
    return a.hasMoney - b.hasMoney;
  } else if(a.active) {
    return -1;
  } else if(b.active) {
    return 1;
  } else {
    return b.hasMoney - a.hasMoney;
  }
})

console.log(people);

Try like this. when both are active (on campare), sort on hasMoney

const sort = (arr) => arr.sort((a, b) => {
  if (a.active === b.active) {
    return b.hasMoney - a.hasMoney;
  }
  return b.active - a.active;
})

const people = [
   {
     active: true,
     hasMoney: false
   },
   {
     active: false,
     hasMoney: false
   },
   {
     active: true,
     hasMoney: true
   }
]

const people2 = [
  { active: true, hasMoney: false },
  { active: false, hasMoney: false },
  { active: false, hasMoney: true },
  { active: true, hasMoney: true },
];



console.log(sort(people))
console.log(sort(people2))

const people = [
   {
     active: true,
     hasMoney: true
   },
   {
     active: false,
     hasMoney: false
   },
   {
     active: true,
     hasMoney: false
   },
   {
     active: false,
     hasMoney: true
   }
]

let peopleSorted = people.sort((a, b) => {
  if (a.active == b.active) {
    if (a.hasMoney < b.hasMoney) {
      return 1;
    } else if (a.hasMoney > b.hasMoney) {
      return -1; 
    } else {
      return 0;
    }
  } else if (a.active) {
    return -1;
  } else if (b.active) {
    return 1;
  } else {
    return 0;
  }
});

console.log(peopleSorted);

The following sort with a OR condition as per provided condition.

const people = [
   {
     active: true,
     hasMoney: false
   },
   {
     active: false,
     hasMoney: false
   },
   {
     active: false,
     hasMoney: true
   },
  {
     active: true,
     hasMoney: false
   }
];

const a = people.sort((a,b) => b.active-a.active || (b.hasMoney - a.hasMoney));

console.log(a);

Related