Save and run as sort.js with a list of integers as arguments
node sort.js 1 3 2 9 3 22
output
[ 1, 2, 3, 3, 9, 22 ]
source
var _ = require('underscore');
var quicksort = function(list) {
if (list.length === 0)
return [];
var p = list[0]; // pivot
var rest = list.slice(1);
var lesser = _.filter(rest, function(x) {
return x<p;
});
var greater = _.filter(rest, function(x) {
return x>=p;
});
return quicksort(lesser).concat(p, quicksort(greater));
};
var list = Array.prototype.slice.call(process.argv, 2)
.map(function(num) {
return parseInt(num);
});
console.log(quicksort(list));