emmoore
2006-03-17 01:40:45 UTC
Hello everybody. I am rather new to Java, and I'm struggling understanding a
program I have to write that needs to use a Comparator. I really don't
understand how to use one or where to put it.
My task is to write a method sort(d) that will sort an ArrayList of points by
x value if d=0 and by y value if d=1. I think I've managed to create the
ArrayList of points needed, but I feel as if I've hit a complete impasse when
it comes to figuring out how to sort in the necessary way.
I've tried doing this a few different ways, all of which are wrong, but maybe
you might help steer me in the correct direction.
One try:
public static ArrayList<Point> sort(final int d)
{
Comparator MyLittlePointComparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
return compare( (Point)o1, (Point)o2);
}
public int compare(Point p, Point q)
{
int diff;
if (d==0)
diff = p.x - q.x;
else if (d==1)
diff = p.y - q.y;
return diff;
}
}
return Collections.sort(this_use_array, MyLittlePointComparator());
Another try:
public static ArrayList<Point> sort(final int d)
{
public class MyLittlePointComparator extends PS7File.EqComparator
implements Comparator<Point>
{
public int compare(Point p, Point q)
{
if (d == 0)
{
double pee = p.getX();
double kyew = q.getX();
}
else if (d == 1)
{
double pee = p.getY();
double kyew = q.getY();
}
if (pee < kyew) return -1;
if (pee == kyew) return 0;
return 1;
}
}
return Collections.sort(this_use_array, new MyLittlePointComparator());
}
}
I just have no idea where to even begin fixing this. The task seems so simple
(just sorting by x and y value), but I feel so lost. Thanks so much in
advance for your time and help.
program I have to write that needs to use a Comparator. I really don't
understand how to use one or where to put it.
My task is to write a method sort(d) that will sort an ArrayList of points by
x value if d=0 and by y value if d=1. I think I've managed to create the
ArrayList of points needed, but I feel as if I've hit a complete impasse when
it comes to figuring out how to sort in the necessary way.
I've tried doing this a few different ways, all of which are wrong, but maybe
you might help steer me in the correct direction.
One try:
public static ArrayList<Point> sort(final int d)
{
Comparator MyLittlePointComparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
return compare( (Point)o1, (Point)o2);
}
public int compare(Point p, Point q)
{
int diff;
if (d==0)
diff = p.x - q.x;
else if (d==1)
diff = p.y - q.y;
return diff;
}
}
return Collections.sort(this_use_array, MyLittlePointComparator());
Another try:
public static ArrayList<Point> sort(final int d)
{
public class MyLittlePointComparator extends PS7File.EqComparator
implements Comparator<Point>
{
public int compare(Point p, Point q)
{
if (d == 0)
{
double pee = p.getX();
double kyew = q.getX();
}
else if (d == 1)
{
double pee = p.getY();
double kyew = q.getY();
}
if (pee < kyew) return -1;
if (pee == kyew) return 0;
return 1;
}
}
return Collections.sort(this_use_array, new MyLittlePointComparator());
}
}
I just have no idea where to even begin fixing this. The task seems so simple
(just sorting by x and y value), but I feel so lost. Thanks so much in
advance for your time and help.