Created
August 16, 2023 15:16
-
-
Save suhailgupta03/c4a8532af073185ab6a05a31d0510702 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | |
// let arr = [3, 56, -1, 0, -45, 899, 78] | |
let arr = ["oranges", "apples", "aaples", "bananas"]; | |
let sorted = arr.sort(); | |
// sort function sorts the array in lexographical order | |
// which means it will sort the array as if it is a string | |
// so the output will be [-1, -45, 0, 3, 56, 78, 899] | |
// because -1 comes before -45, 0 comes before 3, etc. | |
console.log(sorted); | |
let arr1 = [3, 56, -1, 0, -45, 899, 78] | |
let sorted1 = arr.sort((a , b)=> a - b ); | |
console.log(sorted1); | |
// a - b means ascending order | |
// b - a means descending order | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment