package day03;
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.Comparator;
import java.util.List;
public class SortCollection3 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("张三");
list.add("boss");
list.add("苍#null");
list.add("小泽老师");
list.add("范老师");
System.out.println(list);
Comparator<String> com = new MyComparator();
Collections.sort(list,com);
System.out.println(list);
}
}
class MyComparator implements Comparator<String> {
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/08b9571b03da5e371bf52d4a2a6aa0d5.png)