As said, the comparator must return 0 for the same value as identification entails equality (something is always equal to itself), and equality entails equivalence (you can order two different things equivalently, but you must order two equal element is equivalent).
There is one more problem: your check for .X equal to 0 can cause the same elements to return inconsistent ordering if both of them have .X equal to 0. This is an important rule that the comparison method should always be consistent in that :
If x <y, then y> x.
If x <y and y <z, then x <r.
Your algorithm breaks the first rule as follows:
Say that point a is {0, 3} and point b is {0, 2}
A call with (a, b) then returns -1, which means <b, but a call with (b, a) returns -1, which means b <a.
Replace all of this:
lst.Sort(delegate (Point x,Point y) { return (xY * 1.0 / -xX).CompareTo(yY * 1.0 / -yX); });
(Note that this implicitly returns 0 for equal points - we could add an explicit check if this was a harder calculation as an optimization, but this is optional.
Also, Point is System.Drawing.Point here? If so, then this code is now fine, but if it is something else, it is worth noting that the code is fine if Point is a structure, but should contain a null check if Point is a class.
source share