Last active
July 3, 2023 15:59
-
-
Save kironroy/db9b28d8fa3249a98f81e8e55e8a313b to your computer and use it in GitHub Desktop.
sorting scripts
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
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300]; | |
// descending | |
movements.sort((a, b) => { | |
if (a > b) return 1; | |
if (b > a) return -1; | |
}); | |
console.log(movements); | |
// descending refactor | |
movements.sort((a,b) => a - b ); | |
console.log(movements); | |
// ascending | |
movements.sort((a, b) => { | |
if (a > b) return -1; | |
if (b > a) return 1; | |
}); | |
console.log(movements); | |
// ascending refactor | |
movements.sort((a, b) => b - a); | |
console.log(movements); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment