Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 4, 2023 14:48
Show Gist options
  • Save suhailgupta03/bcac0534feeb42e9498262c9d0f1c895 to your computer and use it in GitHub Desktop.
Save suhailgupta03/bcac0534feeb42e9498262c9d0f1c895 to your computer and use it in GitHub Desktop.
let numbers = [100, 0, 99, -1, 999, 4]
let sorted = numbers.sort((a, b) => a - b); // by default sort sorts in ascending order
// when a - b > 0, this means a > b, so a should be after b
// which means that larger number a should be after smaller number b
console.log(sorted)
let sortedDesc = numbers.sort((a, b) => b - a); // sort in descending order
// when b - a > 0, this means b > a, so b should be after a
// which means that larger number b should be after smaller number a
// which will by default mean descending order
console.log(sortedDesc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment