根据elements字段对ArrayList进行排序

use*_*142 1 java arrays sorting arraylist

我有一个对象的arraylist,在对象内部有一个age字段.如何根据年龄按升序对它们进行排序?

谢谢你的时间

Pet*_*rey 10

提供比较例如

Collections.sort(list, new Comparator<MyType>() {
   public int compareTo(MyType t1, MyType t2) {
      return t1.age - t2.age;
   }
}
Run Code Online (Sandbox Code Playgroud)

如果年龄可能很大,这不安全,但我认为年龄将在0到20亿之间.;)