Skip to content

Instantly share code, notes, and snippets.

@tjbrennan
Created January 14, 2015 02:55
Show Gist options
  • Save tjbrennan/7a8098dbe76a4c0adc3d to your computer and use it in GitHub Desktop.
Save tjbrennan/7a8098dbe76a4c0adc3d to your computer and use it in GitHub Desktop.
js sleepsort
var array = [4, 8, 1, 3, 2, 6, 2, 7, 20, 1, 6, 34, 3, 5, 0, 9];
function setTimeoutSort (array, spread) {
var sorted = [];
var i;
for (i = 0; i < array.length; i++) {
(function () {
var el = array[i];
setTimeout(function() {
sorted.push(el);
if (sorted.length === array.length) {
console.log(sorted);
}
}, el * spread);
})();
}
}
setTimeoutSort(array, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment