Skip to content

Instantly share code, notes, and snippets.

@Jagathishrex
Last active January 5, 2021 16:17
Show Gist options
  • Save Jagathishrex/56ee244e55e5cd42342ef4b1fb53b554 to your computer and use it in GitHub Desktop.
Save Jagathishrex/56ee244e55e5cd42342ef4b1fb53b554 to your computer and use it in GitHub Desktop.
sorting numbers
const arr = [1, 20, 8, 4, 9, 0];
//ascending order
arr.sort((a, b) => a - b); // [0, 1, 4, 8, 9, 20]
// Sort in descending order
arr.sort((a, b) => b - a); // [20, 9, 8, 4, 1, 0]
sorting string
const strArr = ['Java', 'Python', 'HTML', "css", "CSS"];
//ascending order
strArr.sort((a, b) => a.localeCompare(b)); // ["css", "CSS", "HTML", "Java", "Python"]
// descending order
strArr.sort((a, b) => b.localeCompare(a)); // ["Python", "Java", "HTML", "CSS", "css"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment