Skip to content

Instantly share code, notes, and snippets.

@kironroy
Last active July 3, 2023 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kironroy/db9b28d8fa3249a98f81e8e55e8a313b to your computer and use it in GitHub Desktop.
Save kironroy/db9b28d8fa3249a98f81e8e55e8a313b to your computer and use it in GitHub Desktop.
sorting scripts
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