Created
June 10, 2020 21:52
-
-
Save sandrabosk/1ecd6535ddd5a482d0ff7d45ffbab37d 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
const unsorted = [1, 4, 2, 11, 42]; | |
console.log(unsorted); | |
const sorted = unsorted.slice() | |
console.log(sorted.sort()); | |
const sorted1 = unsorted.slice().sort((a, b) => a - b); | |
console.log(sorted1); | |
const sorted2 = unsorted.slice().sort((a, b) => b - a); | |
console.log(sorted2); | |
console.log(' ---------------------------------- '); | |
const words = ["Hello", "great", "First", "A", "a", "how"]; | |
words.sort(); | |
console.log(words); | |
const sortedDescWords = words.slice().sort((a, b) => { | |
if(a < b) return 1 | |
if(b < a) return -1 | |
if(a === b) return 0 | |
}); | |
console.log(sortedDescWords); | |
const sortedByLength = words.slice().sort((a, b) => { | |
if(a.length < b.length) return 1 | |
if(b.length < a.length) return -1 | |
if(a.length === b.length) return 0 | |
}) | |
console.log('---> ', sortedByLength) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment