Skip to content

Instantly share code, notes, and snippets.

@stephencweiss
Created December 10, 2018 15:17
Show Gist options
  • Save stephencweiss/75d4cd7307b1e9e22dcfb05154bc3e11 to your computer and use it in GitHub Desktop.
Save stephencweiss/75d4cd7307b1e9e22dcfb05154bc3e11 to your computer and use it in GitHub Desktop.
Using boolean logic to create a simple custom sort -- returning a 1 or -1
/**
* Boolean overview
* if (a < b) {
* // do this because it's true
* } else {
* // do this because it's false
* }
*/
const arr = [4,1,2,5,3,2];
function ascendingSortBool(a,b) {
return +(a > b) || -(a < b)
}
console.log(`Ascending -> `, .sort(ascendingSortBool)) // [ 1, 2, 2, 3, 4, 5 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment