一:实体类Numb
class Numb {
private int a;
private int b;
public Numb() {
}
public Numb(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
二 实现Comparator接口
class NumbSort implements Comparator<Numb>{
@Override
public int compare(Numb c1, Numb c2) {
//根据Numb类的成员变量a排序
if(c1.getA()>c2.getA())
return 1;
return 0;
}
}
三 测试
public class Test {
@org.junit.Test
public void test() {
List<Numb> list=new ArrayList<Numb>();
list.add(new Numb(1,4));
list.add(new Numb(17,2));
list.add(new Numb(22,5));
list.add(new Numb(10,4));
Collections.sort(list, new NumbSort());
for(Numb n:list){
System.out.println(n.getA()+" "+n.getB());
}
}
}
四 结果