1, the entity class implements the comparable interface, the rewriting CompareTo method
Package io;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Student implements comparable<student>{private String name;
private int age;
Private Date birthday;
Public Student (string name, int age, string birthday) {super ();
THIS.name = name;
This.age = age;
DateFormat ds=new SimpleDateFormat ("Yyyy-mm-dd");
try {this.birthday = Ds.parse (birthday);
catch (ParseException e) {e.printstacktrace ();
};
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;
The public void Setbirthday (Date birthday) {this.birthday = birthday;
///Descending by name in ascending birthday @Override public int compareTo (Student o) {int result=0;
Result=-this.name.compareto (O.name); if (result==0) {RESult=this.birthday.compareto (O.birthday);
return result; @Override public String toString () {return "Student [name=] + name +", age= + age +, birthday= "+ Birthday +
"]";
}
}
Package io;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class App. {public
static void Main (string[] args) {
list<student> list=new arraylist<student > ();
List.add (New Student ("John", "1987-10-5"));
List.add (New Student ("Harry", "1986-10-5"));
List.add (New Student ("Zhao Liu", "1988-10-5"));
List.add (New Student ("Wang Qi", "1980-10-5"));
Collections.sort (list);
for (Student s:list) {
System.out.println (s);
}
}
}
2, create a new business class implementation comparator interface, rewrite the Compare method.
package io;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Student {private String name;
private int age;
Private Date birthday;
Public Student (string name, int age, string birthday) {super ();
THIS.name = name;
This.age = age;
DateFormat ds=new SimpleDateFormat ("Yyyy-mm-dd");
try {this.birthday = Ds.parse (birthday);
catch (ParseException e) {e.printstacktrace ();
};
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;
The public void Setbirthday (Date birthday) {this.birthday = birthday; @Override public String toString () {return "Student [name=] + name +", age= + age +, birthday= "+ Birthday
+ "]"; }
}
Package io;
Import Java.util.Comparator;
The public class Birthdaycomp implements comparator<student>{
@Override
the public int compare (Student O1, Student O2) {return
o1.getbirthday (). CompareTo (O2.getbirthday ());
}
}
Package io;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
public class App. {public
static void Main (string[] args) {
list<student> list=new arraylist<student& gt; ();
List.add (New Student ("John", "1987-10-5"));
List.add (New Student ("Harry", "1986-10-5"));
List.add (New Student ("Zhao Liu", "1988-10-5"));
List.add (New Student ("Wang Qi", "1980-10-5"));
Collections.sort (list, new Birthdaycomp ());
for (Student s:list) {
System.out.println (s);
}
}
}