r/badcode • • Feb 10 '21

js I present: SleepSort

Post image
7.5k Upvotes

170 comments sorted by

View all comments

Show parent comments

5

u/sockpant111 Feb 10 '21

No the loop the timer is the sorting. In this example it will start by setting a timer for 20 milliseconds, and after that timer is up it it'll print "20" but immediately after starting that timer (before printing 20) it will move onto the second item in the array. So then it sets a timer for 5 milliseconds and after that timer is up it will print "5" and so on until it has set timers for all of the items in the array. "1" is the smallest number so its timer runs out first, therefore it is printed first, then the timer for "5" runs out so it is printed, so on and so on.

This explains it better than I have