功能:通过java对对象按生日时间进行升序排序
代码块
User .java
package com.yubai;
/**
* Created by bayu on 9/13/2017.
*/
public class User {
private String name;
private String sex;
private int age;
private String birthday;
public User(String name, String sex, int age, String birthday) {
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
SortClass .java
package com.yubai;
import java.util.Comparator;
/**
* Created by bayu on 9/13/2017.
*/
public class SortClass implements Comparator{
@Override
public int compare(Object o1, Object o2) {
User u1 = (User)o1;
User u2 = (User)o2;
return u1.getBirthday().compareTo(u2.getBirthday());
}
}
UserTest .java
package com.yubai;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Created by bayu on 9/13/2017.
*/
public class UserTest {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
userList.add(new User("吴光雷", "男", 25, "19871028"));
userList.add(new User("吴桥", "男", 22, "19900923"));
userList.add(new User("陆晓敏", "男", 25, "19870728"));
userList.add(new User("王佳俊", "男", 23, "19890728"));
userList.add(new User("秦菁", "女", 23, "19891212"));
SortClass sort = new SortClass();
Collections.sort(userList,sort);
for (User u:userList) {
System.out.println(u.getName()+","+u.getSex()+","+u.getAge()+","+u.getBirthday());
}
}
}