Please help...Sort a group of points

hi,i m new.i have an assignment that need to sort a group of points.
the points are in 3d coordinate system.
May anyone help me?
give some suggestion on how to do it.
i know how to sort a group of number,but i have no any idea how to sort a group of points by ascending.
Thanks for your helping.
Last edited on
To sort any objects you need a comparison criteria. In the case of numbers, it is obviously operator <. In the case of points, there is no obvious way. You could sort them by x coordinate then by y (if x1==x2), then by z. You could sort them by the distance from some other point. You could sort them by x+y+z and etc. All of these are the same algorithm, with < replaced by a call to another function bool less(mystruct a, mystruct b). This is probably a part of another assignment. The whole text should give you an idea how to compare two points in a meaningful way.
ok.Thanks. i will try it out.
I think besides an operator < you would also overload an operator ==. With these 2 operators overloaded, the sort function can figure out the rest of the comparison criteria like < <= == > >=
Somehow, I feel sorting the points should be based on the criteria as to how close they are to the origin of the co-ordinate system...
@sohguanh, a == b = !(a<b || b<a). Though I do understand this is up to twice as slow as writing your own ==. On the other hand, sorting doesn't involve much ==, does it?
I don't know. It is just that I saw code on forum and internet, most programs overload operator == and operator <

Based on your expression, we can do away with operator == ?

a==b is equivalent to !(a<b || b<a) ?
Yes, they are the same.

!(a<b) -> a >= b
!(b<a) -> b >= a
--> a must be equal to b
And ! is distributive to ||, hence the notation by hampsterman.
Topic archived. No new replies allowed.