Created
December 10, 2018 15:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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