2011-06-16 14:46:10 +04:00
|
|
|
// sleepsort.js - Sort integers from the commandline in a very ridiculous way: leveraging timeouts :P
|
2015-04-10 10:51:00 +03:00
|
|
|
|
|
|
|
"use strict";
|
2012-01-14 19:13:26 +04:00
|
|
|
var system = require('system');
|
2011-06-16 14:46:10 +04:00
|
|
|
|
|
|
|
function sleepSort(array, callback) {
|
|
|
|
var sortedCount = 0,
|
|
|
|
i, len;
|
|
|
|
for ( i = 0, len = array.length; i < len; ++i ) {
|
2011-06-16 17:48:41 +04:00
|
|
|
setTimeout((function(j){
|
2011-06-16 14:46:10 +04:00
|
|
|
return function() {
|
2011-06-16 17:48:41 +04:00
|
|
|
console.log(array[j]);
|
|
|
|
++sortedCount;
|
|
|
|
(len === sortedCount) && callback();
|
2011-06-16 14:46:10 +04:00
|
|
|
};
|
2011-06-16 17:48:41 +04:00
|
|
|
}(i)), array[i]);
|
2011-06-16 14:46:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 11:18:00 +04:00
|
|
|
if ( system.args.length < 2 ) {
|
2011-06-16 14:46:10 +04:00
|
|
|
console.log("Usage: phantomjs sleepsort.js PUT YOUR INTEGERS HERE SEPARATED BY SPACES");
|
2012-05-16 11:37:26 +04:00
|
|
|
phantom.exit(1);
|
2011-06-16 14:46:10 +04:00
|
|
|
} else {
|
2014-06-27 11:18:00 +04:00
|
|
|
sleepSort(system.args.slice(1), function() {
|
2011-06-16 14:46:10 +04:00
|
|
|
phantom.exit();
|
|
|
|
});
|
2014-06-27 11:18:00 +04:00
|
|
|
}
|