import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Hm_Test1 {
public static void main(String[] args){
ArrayList<User> a=new ArrayList<User>();
a.add(new User("chen",22));
a.add(new User("chen",1));
a.add(new User("chen",12));
a.add(new User("chen",22));
a.add(new User("chen",23));
a.add(new User("chen",222));
Collections.sort(a,new User());
System.out.println(a);
}
}
class User implements Comparator<User>{
String name;
int age;
public User(){}
public User(String name,int age){
this.name=name;
this.age=age;
}
public int compare(User a,User b){
if(a.age<b.age)
return 1;
else if(a.age>b.age)
return -1;
else
return 0;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
} |