Shortest code to sort walking points The challenge is to, given a list of points, sort in a way that, when they are connected in this order, they never intersect.

Input format (read from stdin):

X Y
1 2
3 4
5 6
...


Output should be the same as input, but sorted.

Rules:


You can start from any point.
The last point must be same as first one, making a close circuit.
It should be  done considering the Cartesian grid.
You may assume that the input points do not all lie on a single line.
The endpoints of each line do not count as intersecting.


For further help:
Mathematica - 20 30 chars

Just the sorting part

SortBy[%, ArcTan @@ # &amp;]


Whole testing code, that consists of generating 100 random points and plotting the line

RandomReal[{-10, 10}, {100, 2}];
SortBy[%, ArcTan @@ # &amp;];
ListPlot@% /. 
 Point[p_] :&gt; {EdgeForm[Dashed], FaceForm[White], Polygon[p], 
   PointSize -&gt; Large, Point[p]}


+26 chars

If you demand proper input/ouput, then

"0 0
 4 4
 0 4
 4 0";
Grid@SortBy[%~ImportString~"Table", ArcTan @@ # &amp;]


+2 chars

Just adding N@

Grid@SortBy[%~ImportString~"Table", ArcTan @@ N @ # &amp;]


because above code works only on real numbers and not on integers due to Mathematica's weird behavior when sorting symbolic expressions

N@Sort[{ArcTan[1], ArcTan[-2]}]
Sort[N@{ArcTan[1], ArcTan[-2]}]

{0.785398, -1.10715}
{-1.10715, 0.785398}


EDIT I just realized if the points are not around the origin it won't work, so it also requires shift to the center of points

SortBy[%, ArcTan @@ N[# - Mean@%] &amp;]