public class Tree implements Comparable<Tree>{
private String name = "";
private Integer num = 0;
public Tree(String name, Integer num) {
this.name = name;
this.num = num;
}
@Override
public int compareTo(Tree o) {
return num.compareTo(o.getNum());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
}
这样一个具有被排序能力的类就完成了,那么我们对一个队列进行排序
public static void main(String[] args) {
List<Tree> trees = new ArrayList<Tree>();
trees.add(new Tree("apple", 100));
trees.add(new Tree("banana", 88));
Collections.sort(trees);
}