List排序(数组)

public class ListTest {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        list.add(2);
        list.add(5);
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        for (Integer data : list)
            System.out.println(data);

        //2  测试
        Student student1 = new Student();
        student1.setAge(12);
        student1.setName("张三");
        Student student2 = new Student();
        student2.setName("李四");
        student2.setAge(13);
        Student student3 = new Student();
        student3.setName("王五");
        student3.setAge(14);
        List<Student> students = new ArrayList<Student>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        Collections.sort(students);
        for (Student student: students)
            System.out.println(student.getAge());
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return  o2.getAge() - o1.getAge();
            }
        });
        for (Student student : students)
            System.out.println(student.getAge());
    }
}
发布了5 篇原创文章 · 获赞 0 · 访问量 1376
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览