2015年9月12日 星期六

在java做排序

※範例一

SortedSet<Date> set = new TreeSet<Date>();
set.add(new Date(102, 1, 1));
set.add(new Date());
set.add(new Date(103, 1, 1));
set.add(new Date(100, 1, 1));
set.add(new Date(101, 1, 1));
System.out.println("第一筆=" + set.first());
System.out.println("最後一筆=" + set.last());
System.out.println("排序結果=" + set);

※範例二

List<Date> list = new ArrayList<Date>();
list.add(new Date(102, 1, 1));
list.add(new Date());
list.add(new Date(103, 1, 1));
list.add(new Date(100, 1, 1));
list.add(new Date(101, 1, 1));
System.out.println("排序前=" + list);

Collections.sort(list, new Comparator<Date>() {
    @Override
    public int compare(Date o1, Date o2) {
        if (o1.before(o2)) {
            return 1;
        } else if (o1.after(o2)) {
            return -1;
        } else {
            return 0;
        }
    }
});
System.out.println("排序後=" + list);
※如果假資料是陣列,可用Arrays.sort
※倒序正序可以將-1和1互換

※範例三

class Student {
    private String name;
    private Date learningDate;
    private Integer learningNo;
    //setter/getter
}

List<Student> list = new ArrayList<Student>();
Student stu1 = new Student();
stu1.setName("circle");
stu1.setLearningDate(new Date(101, 1, 1));
stu1.setLearningNo(10);

Student stu2 = new Student();
stu2.setName("bruce");
stu2.setLearningDate(new Date(100, 2, 2));
stu2.setLearningNo(20);

Student stu3 = new Student();
stu3.setName("apple");
stu3.setLearningDate(new Date(102, 3, 3));
stu3.setLearningNo(30);

list.add(stu1);
list.add(stu2);
list.add(stu3);

System.out.println("list1=");
for (Student stu : list) {
    System.out.println(stu.getName());
    System.out.println(stu.getLearningDate());
    System.out.println(stu.getLearningNo());
}

Collections.sort(list, new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getLearningDate().before(o2.getLearningDate())) {
            return -1;
        } else if (o1.getLearningDate().after(o2.getLearningDate())) {
            return 1;
        } else {
            return 0;
        }
    }
});

System.out.println("\nlist2=");
for (Student stu : list) {
    System.out.println(stu.getName());
    System.out.println(stu.getLearningDate());
    System.out.println(stu.getLearningNo());
}
類似從資料庫撈出來的效果

※範例四

class Student {
    private String name;
    private Date learningDate;
    private Integer learningNo;
    //setter/getter
}

class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getLearningDate().before(o2.getLearningDate())) {
            return 1;
        } else if (o1.getLearningDate().after(o2.getLearningDate())) {
            return -1;
        } else {
            return 0;
        }
    }
}

List<Student> list = new ArrayList<Student>();
Student stu1 = new Student();
stu1.setName("circle");
stu1.setLearningDate(new Date(101, 1, 1));
stu1.setLearningNo(10);

Student stu2 = new Student();
stu2.setName("bruce");
stu2.setLearningDate(new Date(100, 2, 2));
stu2.setLearningNo(20);

Student stu3 = new Student();
stu3.setName("apple");
stu3.setLearningDate(new Date(102, 3, 3));
stu3.setLearningNo(30);

list.add(stu1);
list.add(stu2);
list.add(stu3);

System.out.println("list1=");
for (Student stu : list) {
    System.out.println(stu.getName());
    System.out.println(stu.getLearningDate());
    System.out.println(stu.getLearningNo());
}

Collections.sort(list, new StudentComparator());

System.out.println("\nlist2=");
for (Student stu : list) {
    System.out.println(stu.getName());
    System.out.println(stu.getLearningDate());
    System.out.println(stu.getLearningNo());
}
和範例三很像,只是新增一個class,然後將覆寫的方法寫在裡面

沒有留言:

張貼留言