声明:我个子不高,因此手也不怎么'高'方法有二
一:将person对象实现一个public interface Comparable<T>This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective.Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals. One exception is java.math.BigDecimal, whose natural ordering equates BigDecimal objects with equal values and different precisions (such as 4.0 and 4.00).For the mathematically inclined, the relation that defines the natural ordering on a given class C is:       {(x, y) such that x.compareTo(y) <= 0}.
 The quotient for this total order is:       {(x, y) such that x.compareTo(y) == 0}.
 It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method:     {(x, y) such that x.equals(y)}. This interface is a member of the Java Collections Framework.这样的接口
然后直接调用public class Collections
extends ObjectThis class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)The "destructive" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.This class is a member of the Java Collections Framework.Since:
    1.2
See Also:
    Collection, Set, List, Map的max方法二:
直接调用上面那个工具类的static <T> void  sort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
这个方法,自己写一个比较器

解决方案 »

  1.   

    1l方法可行哦,建议使用第二种方法撒,多查API,方法自然就有了
      

  2.   

    class User {
      String name;
      String age;
      
      public User(String name,String age){
       this.name=name;
       this.age=age;
      }
      public String getAge() {
       return age;
      }
      public void setAge(String age) {
       this.age = age;
      }
      public String getName() {
       return name;
      }
      public void setName(String name) {
       this.name = name;
      } 
     } //具体的比较类,实现Comparator接口import java.util.Comparator;
     import java.util.List;
     import java.util.ArrayList;
     import java.util.Collections; public class ComparatorUser implements Comparator{ public int compare(Object arg0, Object arg1) {
       User user0=(User)arg0;
       User user1=(User)arg1;    //首先比较年龄,如果年龄相同,则比较名字  int flag=user0.getAge().compareTo(user1.getAge());
       if(flag==0){
        return user0.getName().compareTo(user1.getName());
       }else{
        return flag;
       }  
      }
      
     }
     //测试类
    public class SortTest { 
      public static void main(String[] args){
       List userlist=new ArrayList();
       userlist.add(new User("dd","4"));
       userlist.add(new User("aa","1"));
       userlist.add(new User("ee","5"));
       userlist.add(new User("bb","2"));  
       userlist.add(new User("ff","5"));
       userlist.add(new User("cc","3"));
       userlist.add(new User("gg","6"));
       
       ComparatorUser comparator=new ComparatorUser();
       Collections.sort(userlist, comparator);
        
       for (int i=0;i<userlist.size();i++){
        User user_temp=(User)userlist.get(i);
           System.out.println(user_temp.getAge()+","+user_temp.getName()); 
       }
       
      }
     }  //首先年龄排序,如果年龄相同,则按名字排序 结果:
        1, aa
        2, bb
        3, cc
        4, dd
        5, ee                    //注意:同样是5岁的人,则比较名字(ee,ff),然后排序
       5, ff
        6, gg
      

  3.   

    public static <T> void sort(List<T> list, Comparator<? super T> c);
    自己写个比较器
      

  4.   

    直接 遍历 list集合 
      

  5.   

    定义一个根据age比较的Comparator,然后
    直接使用 Collections的
    public static <T extends java/lang/Object> T max(java.util.Collection<? extends T>, java.util.Comparator<? super T>);
      

  6.   

    import java.util.*;class Person implements Comparable<Person> {
    private int age;
    public Person(int age) {
    this.age = age;
    }
    public int getAge() {
    return age;
    }
    @Override
    public int compareTo(Person o) {
    if (age < o.getAge()) {
    return -1;
    } else if (age == o.getAge()){
    return 0;
    } else {
    return 1;
    }
    }
    }public class Main {
    public static void main(String[] args) {
    Random r = new Random();
    List<Person> listOfPerson = new LinkedList<Person>();
    for (int i = 0; i < 10; ++i) {
    Person p = new Person(r.nextInt(100));
    listOfPerson.add(p);
    System.out.format("Person[%d].age=%d\n", i, p.getAge());
    }
    Person oldest = Collections.max(listOfPerson);
    System.out.format("Max Age=%d\n", oldest.getAge());
    }
    }