Last active
January 5, 2021 16:17
-
-
Save Jagathishrex/56ee244e55e5cd42342ef4b1fb53b554 to your computer and use it in GitHub Desktop.
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
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