Skip to content

Instantly share code, notes, and snippets.

@nolleto
Created February 11, 2021 23:18
Show Gist options
  • Save nolleto/c9d9b3ed3577a934304655bedd4c6108 to your computer and use it in GitHub Desktop.
Save nolleto/c9d9b3ed3577a934304655bedd4c6108 to your computer and use it in GitHub Desktop.
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