Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式:
public static > void sort(List list) {
list.sort(null);
}
public static void sort(List list, Comparator super T> c) {
list.sort(c);
}
首先使用基本类型(此处使用Integer)来演示第一个方法:
static Integer[] a = {2,3,1};
static List list=new ArrayList(Arrays.asList(a));
public static void main(String[] args) {
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Collections.sort(list);
System.out.println("排序后:"+list);
}
运行结果如下图所示:
可以看到,默认的排序是正序,那么如何实现逆序呢,这就要使用第二种方式了,即通过实现Comparator接口的compare方法来完成自定义排序,代码如下:
static Integer[] a = { 2, 3, 1 };
static List list = new ArrayList(Arrays.asList(a));
public static void main(String[] args) {
System.out.println("排序前:" + list);
System.out.println("读书百遍,其义自见");
Collections.sort(list, new Comparator() {
public int compare(Integer o1, Integer o2) {
// 返回值为int类型,大于0表示正序,小于0表示逆序
return o2 - o1;
}
});
System.out.println("排序后:" + list);
}
运行结果如下图所示:
可以看到,已经实现了逆序的排序了。
接下来看看自定义类的排序:
定义一个Student类:
public class Student {
private int sno; //学号
private String name;//姓名
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int sno, String name) {
super();
this.sno = sno;
this.name = name;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", name=" + name + "]";
}
}
首先使用同样的方式来使用Collections.sort方法:
此时会报错:
Bound mismatch: The generic method sort(List
意思是参数类型为List
根据不同的排序方式即可呈现不同的结果。
1.按照学生学号正序排序
public static void main(String[] args) {
Student stu1 = new Student(2,"至尊宝");
Student stu2 = new Student(3,"凤求凰");
Student stu3 = new Student(1,"永耀之星");
List list = new ArrayList(Arrays.asList(stu1,stu2,stu3));
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Collections.sort(list,new Comparator() {
public int compare(Student o1, Student o2) {
//按学号正序排序
return o1.getSno()-o2.getSno();
}
});
System.out.println("排序后:"+list);
}
运行结果如下图所示:
2.按照学生学号逆序排序
public static void main(String[] args) {
Student stu1 = new Student(2,"至尊宝");
Student stu2 = new Student(3,"凤求凰");
Student stu3 = new Student(1,"永耀之星");
List list = new ArrayList(Arrays.asList(stu1,stu2,stu3));
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Collections.sort(list,new Comparator() {
public int compare(Student o1, Student o2) {
//按学号逆序排序
return o2.getSno()-o1.getSno();
}
});
System.out.println("排序后:"+list);
}
运行结果如下图所示:
3.按照学校姓名正序排序
public static void main(String[] args) {
Student stu1 = new Student(2,"至尊宝");
Student stu2 = new Student(3,"凤求凰");
Student stu3 = new Student(1,"永耀之星");
List list = new ArrayList(Arrays.asList(stu1,stu2,stu3));
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Collections.sort(list,new Comparator() {
public int compare(Student o1, Student o2) {
//按学生姓名正序排序
return o1.getName().compareTo(o2.getName());
}
});
System.out.println("排序后:"+list);
}
运行结果如下图所示:
public static void main(String[] args) {
Student stu1 = new Student(2,"至尊宝");
Student stu2 = new Student(3,"凤求凰");
Student stu3 = new Student(1,"永耀之星");
List list = new ArrayList(Arrays.asList(stu1,stu2,stu3));
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Collections.sort(list,new Comparator() {
public int compare(Student o1, Student o2) {
//按学生姓名逆序排序
return o2.getName().compareTo(o1.getName());
}
});
System.out.println("排序后:"+list);
}
运行结果如下图所示:
接下来看看第一种形式的实现
首先让Student类继承Comparable接口并重写compareTo方法(为了和上面的排序方式区别开,此次按照学生姓名逆序排列):
public class Student implements Comparable{
private int sno;
private String name;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private Student(int sno, String name) {
super();
this.sno = sno;
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + ", sno=" + sno + "]";
}
public int compareTo(Student stu) {
//按学号正序排序
//return this.getSno()-stu.getSno();
//按学号逆序排序
//return stu.getSno()-this.getSno();
//按学生姓名正序排序
//return this.getName().compareTo(stu.getName());
//按学生姓名逆序排序
return stu.getName().compareTo(this.getName());
}
public static void main(String[] args) {
Student stu1 = new Student(2,"至尊宝");
Student stu2 = new Student(3,"凤求凰");
Student stu3 = new Student(1,"永耀之星");
List list = new ArrayList(Arrays.asList(stu1,stu2,stu3));
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Collections.sort(list);
System.out.println("排序后:"+list);
}
}
运行结果如下图所示:
总结:
1.对于String或Integer这些已经实现Comparable接口的类来说,可以直接使用Collections.sort方法传入list参数来实现默认方式(正序)排序;
2.如果不想使用默认方式(正序)排序,可以通过Collections.sort传入第二个参数类型为Comparator来自定义排序规则;
3.对于自定义类型(如本例子中的Student),如果想使用Collections.sort的方式一进行排序,可以通过实现Comparable接口的compareTo方法来进行,如果不实现,则参考第2点;
4.jdk1.8的Comparator接口有很多新增方法,其中有个reversed()方法比较实用,是用来切换正序和逆序的,代码如下:
System.out.println("排序前:"+list);
System.out.println("读书百遍,其义自见");
Comparator comparator =new Comparator() {
public int compare(Student o1, Student o2) {
//按学号正序排序
//return o1.getSno()-o2.getSno();
//按学号逆序排序
//return o2.getSno()-o1.getSno();
//按学生姓名正序排序
//return o1.getName().compareTo(o2.getName());
//按学生姓名逆序排序
return o2.getName().compareTo(o1.getName());
}
};
/*新的排序实现方式*/
Collections.sort(list,comparator.reversed());
System.out.println("排序后:"+list);
运行结果如下图所示:
重写的compare方法定义的是按学生姓名逆序排序,在使用reversed翻转后是按学生姓名正序排序,结果如下: