To find the angle between points you can use vector to calculate it.
First transfer angle between 3 points to angle between to vectors.
Assume you have 3 points: A(xa,ya), B(xb,yb), C(xc,yc)
And if you want to find angle between AB and AC
1. create two vector AB=(xb-xa,yb-ya), AC(xc-xa,yc-ya)
2. inner product between two vectors AB AC =|AB|*|AC|*cos(cta)
You can cllculate cos(cta)= inner product of two vector/ (|AB|*|AC|) to find angle cta
where |AB| is the length of vector AB.
I think you know how to do the sorting.