Loading

Collections.sort的两种用法

http://gwh-08.iteye.com/blog/1233401/

 

class Foo implements Comparable<Foo>{

    

    @Override
    public int compareTo(ClientPortalButton o) {
        return this.i.compareTo(o.getI());
    }

}

 学习了:http://blog.csdn.net/veryisjava/article/details/51675036

可以使用匿名类直接进行排序

public static void main(String[] args) {  
        List<Students> students = new ArrayList<Students>();  
        students.add(new Students(23, 100));  
        students.add(new Students(27, 98));  
        students.add(new Students(29, 99));  
        students.add(new Students(29, 98));  
        students.add(new Students(22, 89));  
        Collections.sort(students, new Comparator<Students>() {  
  
            @Override  
            public int compare(Students o1, Students o2) {  
                int i = o1.getScore() - o2.getScore();  
                if(i == 0){  
                    return o1.getAge() - o2.getAge();  
                }  
                return i;  
            }  
        });  
        for(Students stu : students){  
            System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());  
        }  
}  

 

posted @ 2015-08-26 09:40  stono  阅读(212)  评论(0编辑  收藏  举报