1.在对应实体类中继承comparable<T>,并实现compareTo方法
- 让需要排序的实体类继承comparable<T>
public class Entity implements Comparable<Entity >
- 在类中实现该接口的compareTo方法(让其按时间(time)排序)
@Override
public int compareTo(Entity o) {
if (this.getTime() > o.getTime()) {
return 1;
} else if (this.getTime() < o.getTime()) {
return -1;
} else {
return 0;
}
}
2.使用
Collections.sort(entity);