Java排序
前言:很久没有写排序的方法,最近面试发现回答这类问题有点生疏,特此整理并复习一下相关知识。
一:定义实体对象Cell
public class Cell implements Comparable<Cell> {
private int x;
private int y;
private int z;
@Override
public int compareTo(Cell o) {
if (this.getX() != o.getX()) {
// 若果X不相等,先对x排序
return this.getX() - o.getX();
} else if (this.getY() != o.getY()) {
// 若x相等,然后通过Y排序
return this.getY() - o.getY();
} else {
// 若x和y都相等,然后通过Z排序
return this.getZ() - o.getZ();
}
}
}

![Java排序
[编程语言教程]](https://www.zixueka.com/wp-content/uploads/2024/01/1706713121-a029cd2d5ec5693.jpg)
