package day03;
import java.util.concurrent.Callable;
public class Cell implements Comparable<Cell>{
int x;
private int y;
public Cell(int x,int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "Cell[x=" + x + ",y=" + y + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof Cell) {
Cell c = (Cell) obj;
return this.x==c.x && this.y == c.y;
}
return false;
}
@Override
public int compareTo(Cell o) {
return this.y - o.y;
}
}
package day04;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import day03.Cell;
public class SortCollection2 {
public static void main(String[] args) {
List<Cell> list = new ArrayList<Cell>();
list.add(new Cell(4,5));
list.add(new Cell(1,7));
list.add(new Cell(1,2));
list.add(new Cell(3,3));
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/416dd949eb404183a5044abc4306d48a.png)