Skip to content

Instantly share code, notes, and snippets.

@aeharding
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeharding/7402e718de8359060761 to your computer and use it in GitHub Desktop.
Save aeharding/7402e718de8359060761 to your computer and use it in GitHub Desktop.
Sort an array of positive integers using timeouts. *genius*
# Only works with positive numbers #dealwithit
timeoutSort = (arr, cb) ->
ret = []
longest = 0
for t in arr then do ->
longest = t if t > longest
tmp = t
setTimeout ->
ret.push tmp
, t
setTimeout (-> cb? ret), longest
return
# Tests FTW
timeoutSort [2, 45, 23, 94, 201], (ret) ->
console.assert Array.isArray ret, 'ret is array'
console.assert ret.length is 5, 'five elements long'
expected = [2, 23, 45, 94, 201]
i = expected.length
while i--
console.assert expected[i] is ret[i], "item at index #{i} expected #{expected[i]}, got #{ret[i]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment