常常使用Collection.sort(list)来对list集合进行排序,非常方便:
package 集合.list;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
int a = 8;
int b = 10;
int c = 9;
List list = new ArrayList();
list.add(a);
list.add(b);
list.add(c);
System.out.println("排序前的序列:");
System.out.println(list);
Collections.sort(list);
System.out.println("排序后的序列:");
System.out.println(list);
}
}
这个集合里面是存入的简单类型,所以直接使用了Collections.sort(list)来排序,如果我们想对对象中的某个变量排序呢?
一:Comparable
如果一个类本身实现了Comparable接口,那么把这个类存入到集合list中,就可以使用Collections.sort(list)对这个list进行排序
注意,我们这个实现类中的方法compareto是需要对两个字符串进行比较
package 集合.list.test1;
public class Students implements Comparable{
private String name;
private String score;
public Students(String name,String score){
this.name=name;
this.score=score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@Override
public int compareTo(Students stu) {
// TODO Auto-generated method stub
return this.score.compareTo(stu.getScore());
}
}
测试类:
package 集合.list.test1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsSortTest {
public static void main(String[] args) {
Students stu1=new Students("小明","81");
Students stu2=new Students("小红","83");
Students stu3=new Students("小张","82");
Students stu4=new Students("小王","82");
Students stu5=new Students("小a","82");
Students stu6=new Students("小c","82");
Students stu7=new Students("小b","82");
Listlist=new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
list.add(stu6);
list.add(stu7);
for(int i=0;i
控制台输出如下:
从排序结果看出,我们在方法中要求是使用score来排序,这里从小到大排序,要是score一样的就按存入list顺序输出
二:comparator
如果我们的Students类没有实现这个Comparable类,那么我们可以利用comparator来实现这个类的相关的排序,
这个是用来进行外部排序需要的,有时候我们的一个类封装好了,就不轻易去修改,这种情况就可以使用外部排序方式
package 集合.list.test2;
public class Students {
private String name;
private String score;
public Students(String name,String score){
this.name=name;
this.score=score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
测试类如下:
package 集合.list.test2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import 集合.list.test1.Students;
public class CollectionsSortTest {
public static void main(String[] args) {
Students stu1 = new Students("小明", "81");
Students stu2 = new Students("小红", "83");
Students stu3 = new Students("小张", "82");
Students stu4 = new Students("小王", "82");
Students stu5 = new Students("小a", "82");
Students stu6 = new Students("小c", "82");
Students stu7 = new Students("小b", "82");
List list = new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
list.add(stu6);
list.add(stu7);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i).getName());
System.out.print(list.get(i).getScore() + ";");
}
// 排序
System.out.println();
System.out.println("排序后的list如下");
Collections.sort(list, new Comparator() {
@Override
public int compare(Students o1, Students o2) {
int a = Integer.parseInt(o1.getScore());
int b = Integer.parseInt(o2.getScore());
int n = b - a;
return n;
}
});
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i).getName());
System.out.print(list.get(i).getScore() + ";");
}
}
}
控制台输出如下:
从结果我们看出,这里使用了score进行降序排序