Joining points of polygon in correct order
Start from the leftmost point $x_0$ (the one with minimum $x$-coordinate). If there are ties break them in favor of the point with minimum $y$ coordinate. 

Suppose for simplicity that there are no two points that are collinear with $x_0$ (this case can be handled by suitably breaking ties).

Shoot a ray vertically at an angle of $-\frac{\pi}{2}$ radians. Sweep the plane by rotating the ray anticlockwise up to an angle of $\frac{\pi}{2}$ radians. Whenever the ray intersects a vertex, let this vertex be $x_{i+1}$ where $i$ is the number of intersected vertices so far. Connect $x_{i}$ to $x_{i+1}$.
Finally, connect the last encountered vertex back to $x_0$. 

In practice this boils down to writing all points in polar coordinates from $x_0$, then sorting them by their angle, and connecting them in order.