java中对List中对象排序实现
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); list.add(new Person("张三", 20)); list.add(new Person("李二", 23)); list.add(new Person("赵四", 18)); list.add(new Person("李大", 25)); Collections.sort(list, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return p1.Age > p2.Age ? 1 : (p1.Age == p2.Age ? 0 : -1); } }); for (Person person : list) { System.out.println(person.Name + person.Age); } } static class Person { public Person() { } public Person(String name, int age) { this.Name = name; this.Age = age; } public String Name; public int Age; } } /* 赵四18 张三20 李二23 李大25 */