timeoutSort

19 06 2011

A recent post in 4chan introduced Sleep Sort. The reddit thread that ensued ported it to many languages (from the original bash) but I didn’t see anyone port it to JavaScript yet. So without further ado here’s my port to JavaScript called timeoutSort

Array.prototype.timeoutSort = function(f) {
    this.forEach(function(n) {
        setTimeout(function() {
            f(n)
        }, 5*n)
    })
}

It can be used thus:

[1,9,8,7,6,5,4,3,2,0].timeoutSort(alert)

Or thus:

[1,6,4,3,5,2,0].timeoutSort(function(n){ document.write(n+'<br>')})

Share and enjoy 🙂

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: