package test;
import java.util.ArrayList;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Point Po[]= {new Point(3,4),new Point(2,3),new Point(6,7),new Point(3,9),
new Point(4,5),new Point(7,8),new Point(4,8),new Point(9,4),new Point(4,4),new Point(6,4),};
java.util.Arrays.sort(Po);
for(Point s:Po)
{
System.out.println(s);
}
System.out.println("安装y坐标来排序");
Pointy Po1[]= {new Pointy(3,4),new Pointy(2,3),new Pointy(6,7),new Pointy(3,9),
new Pointy(4,5),new Pointy(7,8),new Pointy(4,8),new Pointy(9,4),new Pointy(4,4),new Pointy(6,4),};
java.util.Arrays.sort(Po1);
for(Pointy s:Po1)
{
System.out.println(s);
}
}
}
package test;
public class Point implements Comparable<Point>{
private double x;
private double y;
Point(double x,double y)
{
this.x=x;
this.y=y;
}
@Override
public int compareTo(Point o) {
if (x > o.x) {
return 1;
} else if (x < o.x) {
return -1;
}
else
{
if(y>o.y)return 1;
else if(y<o.y)return -1;
else return 0;
}
}
public String toString()
{
return "("+x+","+y+")";
}
}
package test;
public class Pointy implements Comparable<Pointy>{
private double x;
private double y;
Pointy(double x,double y)
{
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pointy o) {
if (y > o.y) {
return 1;
} else if (y < o.y) {
return -1;
}
else
{
if(x>o.x)return 1;
else if(x<o.x)return -1;
else return 0;
}
}
public String toString()
{
return "("+x+","+y+")";
}
}