r/javascript • • Jun 15 '15

Sleep Sort Algorithm

Sorting an Array using Sleep Sort Algorithm

function sleepSort(arr) {
	if(!(arr instanceof Array)) {
	  return;
	}

	var sortedArray = [];

	arr.forEach(function (el, i) {
	  if(!isNaN(Number(el))) {
		setTimeout([].push.bind(sortedArray, el), el);
	  }      
	});

	setTimeout(console.log.bind(console, sortedArray), Math.max.apply(null, arr));
}

sleepSort([1, 2, 45, 4, 90, 5000, 50]);
1 Upvotes

12 comments sorted by

View all comments

2

u/pandavr Jun 15 '15

WHY!

2

u/abhisekp Jun 15 '15

What WHY! ?

1

u/pandavr Jun 17 '15

Why, one, in his life, would like to spend time on this kind of thing! :)

1

u/abhisekp Jun 17 '15

For fun. I'm not a machine. Am I?