- import java.util.*;
- public class Test4 {
- public static void main(String[] args)
- {
- List<Person4> list = new ArrayList<Person4>();
- list.add(new Person4("aa", 54));
- list.add(new Person4("cc", 44));
- list.add(new Person4("bb", 70));
- list.add(new Person4("bb", 70));
- list.add(new Person4("ab", 70));
- Collections.sort(list,new Comparator<Person4>()
- {
- @Override
- public int compare(Person4 p1, Person4 p2)
- {
- int num = new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));
-
- if(num == 0)
- return p1.getName().compareTo(p2.getName());
- return num;
- }
- });
- for(Person4 p : list)
- System.out.println(p);
- }
- }
- class Person4
- {
- private String name;
- private int age;
- Person4(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public int getAge()
- {
- return age;
- }
- public String toString()
- {
- return name+" "+age;
- }
-
- }
复制代码 |
|