package hzih.net.modules.quartz.task;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Girl {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Girl() {
}
public Girl(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public static void main(String[] args) {
System.out.println("之前年龄的排序:");
//5个对象集合
List list = new ArrayList<>();
for (int i = 1; i <=5 ; i++) {
Girl girl1 = new Girl(i,"小黑黑"+i,100-i);
System.out.println("姓名:"+girl1.getName()+"-----年龄:"+girl1.getAge());
list.add(girl1);
}
// Collections.sort(list);
// 排序
Collections.sort(list, new Comparator<Girl>() {
@Override
public int compare(Girl o1, Girl o2) {
//按年龄排序
int flag=o1.getAge()-o2.getAge();
return flag;
}
});
System.out.println("排序后的");
for (int i = 0; i <list.size() ; i++) {
Girl girl2=(Girl) list.get(i);
System.out.println("姓名:"+girl2.getName()+"-----年龄:"+girl2.getAge());
}
}
}