Comment by ricardobeat
15 years ago
javascript version for you:
var numbers = process.argv.slice(2)
, output = []
, negative = []
for (var i=0, ln=numbers.length; i<ln; i++){
var n = +numbers[i]
setTimeout((function(n){ return function(){
if (n<0) negative.unshift(n)
else output.push(n)
if (output.length + negative.length === numbers.length) {
return console.log(negative.concat(output))
}
}})(n), Math.abs(n)/1000)
}
Works with negative numbers.
$ node sleepsort -2 1 0.1 4 -1 7 -3 9
[ -3, -2, -1, 0.1, 1, 4, 7, 9 ]
your code can be simplified a bit:
That won't work for numbers < -5 and has a much longer worst case.