1. 首先Person类,实现可按照属性name或者age排序,Personl类如下:
class Person{
public String name;
public int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
2.首先看示例:利用Collections.sort方法实现对String,Integer...等等类型的排序
public class T1 {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("d");
list.add("f");
list.add("a");
sortString(list);
System.out.println("===========");
List<Integer> list2=new ArrayList<Integer>();
list2.add(9);
list2.add(5);
list2.add(8);
sortInteger(list2);
}
public static void sortString(List<String> list){
Collections.sort(list);
for (String string : list) {
System.out.println(string);
}
}
public static void sortInteger(List<Integer> list){
Collections.sort(list);
for (Integer integer : list) {
System.out.println(integer);
}
}
}
3.试试对Person类进行排序
public class T2 {
public static void main(String[] args) {
List<Person> list=new ArrayList<Person>();
Person p1=new Person("d",55);
Person p2=new Person("c",18);
Person p3=new Person("a",37);
list.add(p1);
list.add(p2);
list.add(p3);
sortPerson(list);
}
public static void sortPerson(List<Person> list){
Collections.sort(list);//编译不通过!!!!
}
}
编译器提示"The method sort(List<T>) in the type Collections is not applicable for the arguments (List<Person>)"
要想使用Collections.sort进行排序 该class 必须实现Comparable<Person>接口,并覆写compareTo方法;现在对Person类进行改造:
class Person implements Comparable<Person>{
public String name;
public int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Person o) {
return this.getName().compareTo(o.getName());
}
}
再次测试:
public class T2 {
public static void main(String[] args) {
List<Person> list=new ArrayList<Person>();
Person p1=new Person("d",55);
Person p2=new Person("c",18);
Person p3=new Person("a",37);
list.add(p1);
list.add(p2);
list.add(p3);
sortPerson(list);
}
public static void sortPerson(List<Person> list){
Collections.sort(list);//编译通过;
for (Person person : list) {
System.out.println(person);
}
}
}
输出结果:
Person [name=a, age=37]
Person [name=c, age=18]
Person [name=d, age=55]
4.以上代码中实现了按照name属性对List<Person>排序,如果想按照age属性排序,需要修改>compareTo方法 如下:
@Override
public int compareTo(Person o) {
return new Integer(this.getAge()).compareTo(o.getAge());
}
再次运行main方法测试结果如下:
Person [name=c, age=18]
Person [name=a, age=37]
Person [name=d, age=55]
5.其他实现方式(1):Person类可以不实现Comparable<T>接口,而是调用public static <T> void sort(List<T> list, Comparator<? super T> c)方法实现排序。
public static void sortPerson(List<Person> list){
Collections.sort(list,new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getName().compareTo (o2.getName());
}
});
for (Person person : list) {
System.out.println(person);
}
}
6.其他实现方式(2):Person类可以不实现Comparable < T > 接口,而是通过自定义比较器类的方式实现排序。比较器代码如下:
// 自定义比较器:按name比较
class NameComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return o1.getName().compareTo(o2.getName());
}
}
// 自定义比较器:按age比较
class AgeComparator implements Comparator<Person> {
public int compare(Person object1, Person object2) {// 实现接口中的方法
return new Integer(object1.getAge()).compareTo(object2.getAge());
}
}
比较器使用方式如下:
public class T2 {
public static void main(String[] args) {
List<Person> list=new ArrayList<Person>();
Person p1=new Person("d",55);
Person p2=new Person("c",18);
Person p3=new Person("a",37);
list.add(p1);
list.add(p2);
list.add(p3);
sortPerson(list);
}
public static void sortPerson(List<Person> list){
Collections.sort(list,new NameComparator());
for (Person person : list) {
System.out.println(person);
}
}
}
运行结果如下:
Person [name=a, age=37]
Person [name=c, age=18]
Person [name=d, age=55]