1

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.

3
  • 1
    What order do you want them in? Commented Jan 24, 2023 at 15:49
  • 1
    btw, the callback for sort takes only two parameters. Commented Jan 24, 2023 at 15:52
  • @evolutionxbox true > null > false Commented Jan 24, 2023 at 15:54

3 Answers 3

4

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)
);
Sign up to request clarification or add additional context in comments.

7 Comments

I thought the sort return should be -1, 0, 1? - { true: -1, null: 0, false: 1 }
@evolutionxbox, right, this is working as well, but it prevents using a default value, if you like to move unknown values to top, bottom, or inbetween.
is order in this case the object where the wanted order is saved in? So const order = { true: 1, null: 2, false: 3 }
@MarnixElling, it is. please see edit.
I'm assuming this works if the array had just booleans in it but its an array of objects where 1 of the property of each object is a boolean, i tried to expand order[a] - order[b] into order[a.boolean] - order[b.boolean] but that isn't working, what am i missing?
|
1

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.

Comments

-4

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.

1 Comment

The question is actually asking ways to sort array of object with boolean property rather than to sort boolean array. You could check the question again and improve your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.