📜 ⬆️ ⬇️

Sort by StackSort

A few days ago on xkcd.com a comic was published about inefficient sorting methods. Alt-text to him told about the sorting method StackSort, which is to download from StackOverflow blocks of code that can be found on request "sort the list" and run one by one until you find a workable version. Rave? Still not nonsense! Meet the Gitchhab JavaScript implementation of StackSort .

From StackOverflow, the script downloads top-rated answers to questions marked with “sort” and “javascript” tags, and tries to execute the last block of code in the answer, assuming that this is the final version of the algorithm. Yes, exactly, it takes a piece of code from the Internet and does eval (), so be careful! However, the author tried to do everything possible so that the algorithm was safe - he takes the code only from the answers that are marked as accepted, and only from those that were published before the appearance of the XKCD comic, so that someone does not intentionally insert the malicious code.

If the code block is executed without errors and returns an array, the result is shown to the user. If it is wrong, you can try further. The algorithm works with both numbers and strings and JSON objects. As for efficiency, in the Chromium browser under Ubuntu, the list of several numbers is sorted for 5-10 seconds (in Firefox, StackSort did not work for me). Slowly, but very large and universal, using the collective mind of the entire Internet.

UPD: Thanks to Athari for the list of running solutions compiled using StackSort:
')
Answers work:
stackoverflow.com/questions/12137690/javascript-sort-sparse-array-keep-indexes#12137767
stackoverflow.com/questions/14761032/infinite-recursion-in-javascript-quicksort#14761203
stackoverflow.com/questions/8175093/simple-function-to-sort-a-json-object-using-javascript # 8175221
stackoverflow.com/questions/9280360/javascript-function-returning-unique-values-sorted-by-count#9280565

Partially working answers:
stackoverflow.com/questions/1359761/sorting-a-javascript-object#1359808

Answers do not work (among those that started and returned an array):
stackoverflow.com/questions/4833651/javascript-array-sort-and-unique#4833835
stackoverflow.com/questions/5326880/sort-array-keys-by-value#5327363

Completed 6 pages, then it turned out: "Out of answers from StackOverflow!"

To all the answers there are badly received comments about StackSort. :)

Source: https://habr.com/ru/post/173333/


All Articles