public class TestSort implements Comparator {
public static void main(String[] args) throws ParseException {
TestSort testSort = new TestSort();
List<User> list = new ArrayList<User>();
testSort.addUser(list);
Collections.sort(list,testSort);
for(User o : list) {
System.out.println(o.getName() + "--"+o.getBirthday());
}
}
@Override
public int compare(Object ob1, Object ob2) {
User u1 = (User)ob1;
User u2 = (User)ob2;
Date d1 = u1.getBirthday();
Date d2 = u2.getBirthday();
System.out.println("sort --------------");
if(d1.before(d2)) {
return 1;
}else {
return -1;
}
}
public void addUser(List list) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
String d1 = "2015-08-12 15:51:44";
String d2 = "20150812155059";
String d3 = "20150812155259";
Date date1 = sdf.parse(d1);
Date date2 = sdf2.parse(d2);
Date date3 = sdf2.parse(d3);
User u1 = new User();
User u2 = new User();
User u3 = new User();
u1.setAge(25);
u1.setBirthday(date1);
u1.setName("张一");
u2.setAge(28);
u2.setBirthday(date2);
u2.setName("张二");
u3.setAge(29);
u3.setBirthday(date3);
u3.setName("张三");
list.add(u1);
list.add(u2);
list.add(u3);
}
class User {
private String name;
private int age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
}[code="java"][/code]
public static void main(String[] args) throws ParseException {
TestSort testSort = new TestSort();
List<User> list = new ArrayList<User>();
testSort.addUser(list);
Collections.sort(list,testSort);
for(User o : list) {
System.out.println(o.getName() + "--"+o.getBirthday());
}
}
@Override
public int compare(Object ob1, Object ob2) {
User u1 = (User)ob1;
User u2 = (User)ob2;
Date d1 = u1.getBirthday();
Date d2 = u2.getBirthday();
System.out.println("sort --------------");
if(d1.before(d2)) {
return 1;
}else {
return -1;
}
}
public void addUser(List list) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
String d1 = "2015-08-12 15:51:44";
String d2 = "20150812155059";
String d3 = "20150812155259";
Date date1 = sdf.parse(d1);
Date date2 = sdf2.parse(d2);
Date date3 = sdf2.parse(d3);
User u1 = new User();
User u2 = new User();
User u3 = new User();
u1.setAge(25);
u1.setBirthday(date1);
u1.setName("张一");
u2.setAge(28);
u2.setBirthday(date2);
u2.setName("张二");
u3.setAge(29);
u3.setBirthday(date3);
u3.setName("张三");
list.add(u1);
list.add(u2);
list.add(u3);
}
class User {
private String name;
private int age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
}[code="java"][/code]