Implementation of xkcd's StackSort