Sorting arrays in a common use case in application development. Sorting arrays by a boolean value is not as typical as sorting by numbers or strings. At least for us at Future Studio.
This tutorial shows you how to sort a JavaScript boolean array. Also: this approach works in TypeScript, too!
Node.js Series Overview
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- Write a JSON Object to a File
- How to Create an Empty File
- Generate a Random Number in Range With JavaScript/Node.js
- How to Merge Objects
- Retrieve a Request’s IP Address in Node.js
- Detect the Node.js Version in a Running Process or App
- Install Dependencies for a Specific Package
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
- Generate a Random String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
- How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
- Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js (Coming soon)
- Append an Item at the End of an Array in JavaScript or Node.js (Coming soon)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
- Detect if Value Is a Promise in Node.js and JavaScript
- How to Check if an Object is Empty in JavaScript or Node.js
- How to CamelCase Keys of an Object in JavaScript or Node.js (Coming soon)
Sort a Boolean Array in JavaScript or TypeScript
The .sort()
method in JavaScript or Typescript accepts a comparator function providing you two values of the array.
When sorting booleans, you can short-circuit the comparator and only use a single value. The single boolean value is enough to check whether it’s “true” or “false” and return 1
(value comes first) or -1
(value comes last):
[true, false, true, false].sort(value => {
return value ? 1 : -1 // `false` values first
})
// [ false, false, true, true ]
Depending on your sort direction, you may want the true
values first. Switch the return values in the comparator and return -1
if you want all “true” value to come first:
[true, false, true, false].sort(value => {
return value ? -1 : 1 // `true` values first
})
// [ true, true, false, false ]
This Code Snippet Works in TypeScript
TypeScript requires you to run arithmetic operations on one of these types: any, number, bigint, or enum. The TypeScript linter fails when sorting a boolean array using a - b
(a minus b).
Checking whether the given value is true and returning a number works fine in TypeScript. Sweet!