Created
February 11, 2021 23:18
-
-
Save nolleto/c9d9b3ed3577a934304655bedd4c6108 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
const sleepSort = (arr) => { | |
const { length } = arr | |
const result = [] | |
return new Promise((resolve) => { | |
for (let index = 0; index < length; index++) { | |
const value = arr[index]; | |
setTimeout(() => { | |
result.push(value) | |
const isResultFull = result.length === length | |
if (isResultFull) resolve(result) | |
}, value) | |
} | |
}) | |
} | |
const array = [3, 10, 7, 3, 1, 76, 53, 29, 4, 23] | |
sleepSort(array) | |
.then(sortedArray => { | |
console.log(sortedArray) // [ 1, 3, 3, 4, 7, 10, 23, 29, 53, 76 ] | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment