Last updated: Mar 6, 2024
Reading timeยท5 min
To sort an array of objects by a boolean property:
sort()
method on the array, passing it a function.sort
method will sort the array in place, by the boolean property.const arr = [ {bool: true}, {bool: false}, {bool: false}, {bool: true}, ]; // โ true values first const trueFirst = arr.sort( (a, b) => Number(b.bool) - Number(a.bool), ); // ๐๏ธ [{bool: true}, {bool: true}, {bool: false}, {bool: false}] console.log(trueFirst); // ------------------------------------------------------ // โ false values first const falseFirst = arr.sort( (a, b) => Number(a.bool) - Number(b.bool), ); // ๐๏ธ [{bool: false}, {bool: false}, {bool: true}, {bool: true}] console.log(falseFirst);
We used the Array.sort() method to sort an array of objects by a boolean property.
sort()
method sorts the elements of the array in place and returns the sorted array. In other words, it mutates the original array.const arr = [{bool: true}, {bool: false}, {bool: false}, {bool: true}]; // โ true values first const trueFirst = arr.sort((a, b) => Number(b.bool) - Number(a.bool)); // ๐๏ธ [{bool: true}, {bool: true}, {bool: false}, {bool: false}] console.log(trueFirst); // ๐๏ธ [{bool: true}, {bool: true}, {bool: false}, {bool: false}] // ๐๏ธ (Original array also changed) console.log(arr);
If you want to sort the array without mutating it, use the spread syntax (...)
to create a shallow copy before calling the sort()
method.
const arr = [ {bool: true}, {bool: false}, {bool: false}, {bool: true}, ]; // โ true values first const trueFirst = [...arr].sort( (a, b) => Number(b.bool) - Number(a.bool), ); // ๐๏ธ [ { bool: true }, { bool: true }, { bool: false }, { bool: false } ] console.log(trueFirst); // โ false values first const falseFirst = [...arr].sort( (a, b) => Number(a.bool) - Number(b.bool), ); // ๐๏ธ [ { bool: false }, { bool: false }, { bool: true }, { bool: true } ] console.log(falseFirst); // ๐๏ธ [ { bool: true }, { bool: false }, { bool: false }, { bool: true } ] console.log(arr);
We used the
spread syntax (...) to
unpack the values of the array into a new array before calling the sort
method.
The sort()
method takes a function that defines the sort order as a parameter.
const arr = [{bool: true}, {bool: false}, {bool: false}, {bool: true}]; // โ true values first const trueFirst = arr.sort((a, b) => Number(b.bool) - Number(a.bool)); // โ false values first const falseFirst = arr.sort((a, b) => Number(a.bool) - Number(b.bool));
The sort()
method uses the following logic to sort the elements in the array:
If the return value of the compare function is greater than 0
, then sort b
before a
.
If the return value of the compare function is less than 0
, then sort a
before b
.
If the return value of the compare function is equal to 0
, keep the original
order of a
and b
.
When converting booleans to a number, true
gets converted to 1
, and false
gets converted to 0
.
console.log(Number(true)); // ๐๏ธ 1 console.log(Number(false)); // ๐๏ธ 0
In the true
values first example:
a.bool
from b.bool
returns 1
, then b
is true
and a
is false
, so we sort b
before a
.-1
, then b
is false
and a
is true
, so we sort b
after
a
.0
, we keep the original order of a
and b
.To sort an array of booleans:
sort()
method on the array, passing it a function.const arr = [true, false, true, false, false]; // โ true values first const trueFirst = arr.sort((a, b) => Number(b) - Number(a)); // ๐๏ธ [true, true, false, false, false] console.log(trueFirst); // โ false values first const falseFirst = arr.sort((a, b) => Number(a) - Number(b)); // ๐๏ธ [false, false, false, true, true] console.log(falseFirst);
We used the Array.sort() method to sort an array of booleans.
sort()
method sorts the elements of the array in place and returns the sorted array. In other words, it mutates the original array.const arr = [true, false, true, false, false]; // โ true values first const trueFirst = arr.sort((a, b) => Number(b) - Number(a)); // ๐๏ธ [true, true, false, false, false] console.log(trueFirst); // ๐๏ธ [true, true, false, false, false] // ๐๏ธ Original array also changed console.log(arr);
If you want to sort the array without mutating it, use the spread syntax (...)
to create a shallow copy before calling the sort()
method.
const arr = [true, false, true, false, false]; // โ true values first // ๐๏ธ create shallow copy first const trueFirst = [...arr].sort((a, b) => Number(b) - Number(a));
We used the
spread syntax (...) to
unpack the values of the array into a new array before calling the sort
method.
The sort()
method takes a function that defines the sort order as a parameter.
const arr = [true, false, true, false, false]; // โ true values first const trueFirst = arr.sort((a, b) => Number(b) - Number(a)); // โ false values first const falseFirst = arr.sort((a, b) => Number(a) - Number(b));
The sort()
method uses the following logic to sort the elements in the array:
If the return value of the compare function is greater than 0
, then sort b
before a
.
If the return value of the compare function is less than 0
, then sort a
before b
.
If the return value of the compare function is equal to 0
, keep the original
order of a
and b
.
When converting booleans to a number, we get 1
for true
values and 0
for
false
values.
console.log(Number(true)); // ๐๏ธ 1 console.log(Number(false)); // ๐๏ธ 0
In the true
values first example:
a
from b
returns 1
, then b
is true
and a
is
false
, so we sort b
before a
.-1
, then b
is false
and a
is true
, so we sort b
after
a
.0
, we keep the original order of a
and b
.const arr = [true, false, true, false, false]; // โ true values first const trueFirst = arr.sort((a, b) => Number(b) - Number(a));
You can learn more about the related topics by checking out the following tutorials: