# Thread: Problem with comparator implementation

1. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

## Problem with comparator implementation

hi,
this one is driving me crazy

i have made a Point class that holds x and y coordinates. I am calculating the slopes among points and then i want to sort the Points according to their slopes. I have made i private class that Implements comparator for this reason.
This is what i have written

XML Code:
```import java.util.Comparator;

public class Point {

Integer x,y;
public final Comparator <Point>BY_SLOPE=new BySlope();
public Point(Integer x,Integer y){
this.x=x;
this.y=y;
}

public void setX(Integer x){
this.x=x;
}

public int getX(){
return this.x;
}

public void setY(Integer y){
this.y=y;
}

public int getY(){
return this.y;
}

public double slopeTo(Point that){
double a=1;
double pos=(a-a)/a;//horizontal lines
double neg=(a-a)/-a;//vertical lines
double y=that.getY()-this.getY();
double x=that.getX()-this.getX();
double slope=y/x;//(that.getY()-this.getY())/(that.getX()-this.getX());
if (y==0)
slope=pos;
if (x==0)
slope=Double.POSITIVE_INFINITY;
if (y==0 && x==0)
slope=Double.NEGATIVE_INFINITY;
return slope;
}

private class BySlope implements Comparator<Point>{

private double epsilon=0.00001;
@Override
public int compare(Point o1, Point o2) {
double s1=Point.this.slopeTo(o1);
double s2=Point.this.slopeTo(o2);
if (s1<s2)
return 1;
return 0;
}

}

public String toString(){
return ""+this.getX()+","+this.getY();
}
}```

XML Code:
```import java.util.Arrays;

public class PointTest {

public static void main(String[] args) {

int first=0;
int second=1;
int ff=0;
int ss=1;
Point[] arr=new Point [7];

Point a=new Point(23,43);
Point b=new Point(33,3);
Point c=new Point(65,31);
Point d=new Point(14,56);
Point e=new Point(90,0);
Point f=new Point(4,6);
Point g=new Point (35,5);
arr[0]=a;
arr[1]=b;
arr[2]=c;
arr[3]=d;
arr[4]=e;
arr[5]=f;
arr[6]=g;

for (int i=0; i<arr.length-1; i++){
System.out.println(arr[second].getY()+"-"+arr[first].getY()+"/"+arr[second].getX()+"-"+arr[first].getX()+" slope-->> "+arr[second].slopeTo(arr[first]));
first++;
second++;

}
Arrays.sort(arr,arr[0].BY_SLOPE);
System.out.println("----------------------AFTER SORT--------------------");
for (int i=0; i<arr.length-1; i++){
System.out.println(arr[ss].getY()+"-"+arr[ff].getY()+"/"+arr[ss].getX()+"-"+arr[ff].getX()+" slope-->> "+arr[ss].slopeTo(arr[ff]));
ff++;
ss++;
}

}
}```
i really cannot understand why this does not work. Any ideas?

thanks

2. ## Re: Problem with comparator implementation

What exactly do you mean when you say this does not work?

But it seems strange to me to order points by their slope- points don't have a slope. You're introducing a dependence that I'm pretty sure isn't compatible with the notion of sorting.

3. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

## Re: Problem with comparator implementation

Originally Posted by KevinWorkman
What exactly do you mean when you say this does not work?

But it seems strange to me to order points by their slope- points don't have a slope. You're introducing a dependence that I'm pretty sure isn't compatible with the notion of sorting.
hi and thanks for the reply..

what am i trying to do is, lets say your are in a spreadsheet with 3 columns. x,y and their slopes. Then you sort all the columns by the slopes column.

example in libreoffice

XML Code:
```x	y	slope
23	43	-4
33	3	0.875
65	31	-0.49
14	56	-0.736
90	0	0.069
4	6	-0.0322
35	5	0```

turns to this:

XML Code:
```x	y	slope
23	43	-4
14	56	-0.736
65	31	-0.49
4	6	-0.0322
35	5	0
90	0	0.069
33	3	0.875```
see what i mean?

this ia actually part from a very interesting exercise i found here

i the above code i have made a mistake. I shouldn`t recalculate the slope after the array had been sorted but i think you get my point
Last edited by dimiro; 10-04-2012 at 11:17 AM.

4. ## Re: Problem with comparator implementation

Right but the thing is that a point's slope is completely dependent on the other point. So does comparing 2 points and then comparing 1 of those points to a 3rd point tell you about the total ordering of them? What if you had compared them in a different order?

You still haven't told us what doesn't work about your code though.

5. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

## Re: Problem with comparator implementation

exactly they are dependent but i am only calculating the slopes once with their specific initial order . By having them is this *specific* order, calculate the slopes and sort them according to the *already* calculated slopes.

the mistake in the code is that i get this:
23,43
33,3
65,31
14,56
90,0
4,6
35,5
----------------------AFTER SORT--------------------
4,6
65,31
90,0
14,56
35,5
33,3
23,43
while the correct order is the one i mentioned in my previous post.

6. ## Re: Problem with comparator implementation

Originally Posted by dimiro
exactly they are dependent but i am only calculating the slopes once with their specific initial order . By having them is this *specific* order, calculate the slopes and sort them according to the *already* calculated slopes.
That isn't necessarily true. You don't have any control over what order the slopes are compared, especially as they are moved around in the array by whatever sort algorithm Java's using under the hood (I think it's something similar to merge sort, but it doesn't really matter). The point is that you don't have any control over when the points are compared, so you're relying on a dependency that isn't a guarantee.

7. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Problem with comparator implementation

The phrase "non-deterministic" comes to mind.

8. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

## Re: Problem with comparator implementation

That isn't necessarily true. You don't have any control over what order the slopes are compared, especially as they are moved around in the array by whatever sort algorithm Java's using under the hood (I think it's something similar to merge sort, but it doesn't really matter). The point is that you don't have any control over when the points are compared, so you're relying on a dependency that isn't a guarantee.
aaa ok..many thanks for making it clearer. Now i got it

so it makes sense to do something like this:

create a copy of my initial array. Lets say i call it sorted and then i do:

Java Code:
`Arrays.sort(arr,sorted[0].BY_SLOPE);`

i only added a copy constructor in my Point class

Java Code:
```public Point(Point p){
x=p.getX();
y=p.getY();
}```
and initialized a new array

Java Code:
```Point [] sorted=new Point[7];
for (int i=0; i<sorted.length; i++){
sorted[i]=new Point(arr[i]);
}```
but still no luck.
Does this makes sense or am i just trying to do something that cannot be done (like this)?

9. Member
Join Date
Jul 2012
Posts
2
Rep Power
0

aaaa

10. ## Re: Problem with comparator implementation

I still don't really understand what you're trying to do with sorting the points by slope.

What I guess you're trying to do is sort points according to the slope of the imagined function that they all represent at that x-value. But your data doesn't reflect that.

Try sorting them according to x-value, then you can find the "slope of the line to the next point" for each point. Store that in a data structure along with the point. Sort that data structure by the "slope-at-point" value. I still don't know what that gets you, but I think it's what you're trying to do.

11. Member
Join Date
Apr 2011
Posts
7
Rep Power
0

## Re: Problem with comparator implementation

yes this is exactly what am i trying to do. I provided the link to the exercise...

i just put the code (without the sorting according to X comparable) of a toy example with 7 random points so as not to to make this thread huge.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•