
Stacksort – Searches StackOverflow for sorting functions and runs them (2013) - colinprince
http://gkoberger.github.io/stacksort/
======
dahart
Hilariously awesome.

I'm curious whether the multiple warnings about running untrusted code in the
browser are necessary. I feel like all websites are already untrusted code,
and the browser is quite well sandboxed and protected from anything too bad
happening. What is the worst case scenario here for the user within the JS
ecosystem, under known avenues of attack, not counting an unknown zero day
browser exploit?

~~~
duxup
Reminds me of "security expert" warnings in the mass media where every time
something happens they tell people effectively the same thing "don't do things
unless you know / trust it".

Most people have no clue what they're running and can't possibly take the time
to know enough to really have a clue.

~~~
ndnxhs
Trust is fairly worthless as well. Malware devs buy trusted software to turn
in to malware so something that was good and trusted for years is now bad with
no warning.

------
gkoberger
Hey, creator here! I built this a few years ago on a whim, and am surprised
how well it still works. Thanks for sharing again :)

(Psst, if you're an engineer and like dev tools, I'm hiring!
[https://readme.io/careers](https://readme.io/careers))

~~~
munk-a
Is it terrible that I am highly tempted to use this as an API, forcing the
value in via headless chrome then printing the page to a PDF and using OCR &
regular expressions to extract a sorted list? I'm pretty sure that's an O(1)
(ish, not really) which I always heard was the best kind.

~~~
creatornator
It's not O(1). You are only calling the API once, but if you put in a larger
list the time will increase in accordance to whatever the complexity is of the
sorting happening behind the scenes. Calling this O(1) would be like saying
qsort is O(1) because you are only calling the function once.

~~~
your-nanny
dude.its called a joke

------
_ZeD_
this reminds me of the 4chan's sleepsort: for each number $n in the array,
spin a thread that sleeps $n and then append $n to the result array.

~~~
diabeetusman
Would the algorithmic runtime of this be O(N)?

~~~
mortehu
The O(N log N) best case only applies to comparison sorts. What's described
here is not a comparison sort, and could easily be made O(N) with radix sort
if the size of the numeric type is constant.

------
delinka
Is it just me, or do none of them sort this array correctly?

["zebra","apple","banana","5","bapple","banana","banana"]

Correction, the page finally found this algorithm that sorts my array[1]. So I
am disappointed with the verifier function on this web page and may need to
submit a PR

1 -
[http://stackoverflow.com/questions/3730510/#3730579](http://stackoverflow.com/questions/3730510/#3730579)

~~~
hirsin
That conversation about for..in - any JS experts that can say whether modules
or other new advances will help with this? Not being able to use the standard
functions on primitives because random other code messes with them sounds like
pogo sticking in a minefield.

~~~
tomjakubowski

        for (let x of xs)
    

in more recent versions of JS is how you iterate an array without facing that
prototype pollution problem.

See [https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Refe...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Statements/for...of)

~~~
hirsin
Good to know, thanks!

------
CarVac
It only checks that the output is sorted, not that it's a sorted version of
the original string.

The first answer that apparently works has a comment stating that it fails
with more than 2 duplicates; indeed I tried a list with three 3s and the
resulting passing answer only had one of those threes.

~~~
amelius
So a function that always returns an empty list passes the test?

------
colinprince
See also previous discussion:

[https://news.ycombinator.com/item?id=5395463](https://news.ycombinator.com/item?id=5395463)

------
jamesjennings
Wow! I was inspired by this back in 2013 - I hacked a version together that
also took a second argument, which was a description of the operation to be
done (so it wasn't always "Sort"). Worked surprisingly well.

[https://github.com/jamesjennings/stacksolve](https://github.com/jamesjennings/stacksolve)

I cringe at the code now, but still think the idea is neat.

------
52-6F-62
Wow. Hadn't seen this before. I clicked the button and laughed audibly and
loudly at the alert.

------
asciimike
Relevant Python library ([https://github.com/drathier/stack-overflow-
import](https://github.com/drathier/stack-overflow-import)) and previous HN
discussion
([https://news.ycombinator.com/item?id=12868946](https://news.ycombinator.com/item?id=12868946)).

------
coreyp_1
THIS IS WRONG! DON'T DO IT!

Of course, the first thing that I did was to try it in a safe environment. It
worked!!! :)

------
miguelmota
Doesn't seem to work. Tried with input ["z","b","a","c","d"] and returns the
same ["z","b","a" "c","d"] as the output, using StackOverflow answer 4833835

~~~
gamedna
For string sorts, i needed to keep running about 5 or 6 times until a sorted
result was found.

~~~
miguelmota
Ah you're absolutely right, didn't see the "Didn't work? Try the next answer"
button

------
gamedna
This project is both Awesome and Awful at the same time. Love it!

------
quickthrower2
Pure eval

------
admay
Click the button and PRAY this thing doesn't load any sketchy code

~~~
mjlee
I don't think that's so different from normal web browsing. (Normal being
javascript enabled, etc.)

------
quux
Some say it's still running bogosort to this day...

------
age_bronze
Wonder how long until this code pops up in some CTF....

------
sam0x17
How does this work? I thought CORS prevents this?

~~~
ChrisSD
[https://api.stackexchange.com/](https://api.stackexchange.com/)

~~~
sam0x17
ah, I see

------
snek
this should be tagged [2015]

~~~
fredley
[2013], at least: [https://stackoverflow.com/questions/12137690/javascript-
sort...](https://stackoverflow.com/questions/12137690/javascript-sort-sparse-
array-keep-indexes#comment21918994_12137767)

~~~
PhasmaFelis
An attempt was made, but right now it's at "(2103)". Nice to know xkcd will
still be around in the next century!

------
graphememes
The worst part about this is that there is an existing thread from 2013
directly on the site linked.

------
boardmad
This made me squeeee

Well done

------
forgot-my-pw
More crazy sorting techniques from a recent reddit thread:
[https://redd.it/9s9kgn](https://redd.it/9s9kgn)

Highlights:

\- GenghisKhanSort: delete all elements except for the first, repopulate the
list with successors of the first element \- HitlerSort: Choose an element in
the list you think is the best, then loop through the list removing any
element that does not match. \- ThanosSort: delete half the array. The arrays
may or not be sorted, but it'll help for future sorting \- TrumpSort O(0): the
array is always sorted. Anyone who says otherwise is fake news.

