Home > OS >  How to sort array of objects with boolean values: true, false and null
How to sort array of objects with boolean values: true, false and null

Time:01-25

Hi i have an array of objects that i want to sort based on a boolean that one of the objects has. However normally there would be either true or false but in this case we also check on null values because sometimes the data has not been set and in that case we wanna show that it has yet to be set with an icon.

Here's an example of the array:

const arrayOfObjects = [
  {
    id: 69,
    boolean: true,
    name: 'foo',
  },
  {
    id: 42,
    boolean: false,
    name: 'bar',
  },
  {
    id: 666,
    boolean: null,
    name: 'foo',
  },
  {
    id: 420,
    boolean: false,
    name: 'bar',
  },
  {
    id: 2,
    boolean: null,
    name: 'foo',
  },
  {
    id: 123,
    boolean: true,
    name: 'foo',
  },
]

So what i tried first was:

arrayOfObjects.sort((a, b) => b.boolean - a.boolean);

This sets the objects that are true at the front but the objects with false or null are scattered.

Then i tried:

arrayOfObjects.sort((a, b, c) => (c.boolean - b.boolean) - a.boolean);

This just didn't work at all.

I couldn't really find a case that was similar enough to base a solution off of it so hopefully i can find it here.

CodePudding user response:

If you like to use a custom sorting, you could take an object with the wanted sorting, like

const
    order = { true: 1, null: 2, false: 3 };
    data = [{ id: 69, boolean: true, name: 'foo' }, { id: 42, boolean: false, name: 'bar' }, { id: 666, boolean: null, name: 'foo' }, { id: 420, boolean: false, name: 'bar' }, { id: 2, boolean: null, name: 'foo' }, { id: 123, boolean: true, name: 'foo' }];

data.sort((a, b) => order[a.boolean] - order[b.boolean]);

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

If you have unknown values and want to move them to bottom, you could add another key with a large value, like

order = { true: 1, null: 2, false: 3, bottom: Number.MAX_VALUE };

Usage:

data.sort((a, b) =>
    (order[a.boolean] || order.bottom) - (order[b.boolean] || order.bottom)
);

CodePudding user response:

You can check for the null explicitly ...

let list = [{i: 0, boolean: true}, { i: 1, boolean: null}, { i:2, boolean: false}, { i: 4, boolean: true}]

function cpBoolWithNull(a,b) {
  //if both are null return 0 to maintain a stable sort
  //if only one is null return 0 or 1 depending on the value of the other
  if (a.boolean === null) return b.boolean === null ? 0 : b.boolean ? 1 : -1;
  if (b.boolean === null) return a.boolean ? -1 : 1;

  //if both are different from null, sort true before false
  return b.boolean - a.boolean
}

console.log(list.sort(cpBoolWithNull));

This will sort true ... null ... false If you need a differnt order, adjust the return values.

CodePudding user response:

I think that you can have a type checker with JS with this simple script.

let array =[true, false, null];
function check(i){
if (array[i] != null||array[i]!=false){ 

 if (array[i]!=null || array[i]!=true)document.write(" Array item" " " i " " "has the value of boolean false. ");

 if (array[i]!=true||array[i]!=false)document.write(" Array item" " " i " " "has the value of boolean true. ");

if (array[i] != true || array[i] != false  )document.write(" Array item" " " i " " "has the value of object null. ");
document.write("<br>")
}
}
check(0);
   

You can comment out the other text when it is not needed.

  • Related