Java List 排序的2种方法

1. 利用Collections类的 java.util.Collections.sort(java.util.List, java.util.Comparator) 方法,自定义比较器对象对指定对象进行排序

对学生对象按照其 分数(降序)进行排序,当分数相同时按学号(从小到大)排序,代码如下:

Student类

class Student{
	private int id;
	private String name;
	private float score;	//成绩
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getScore() {
		return score;
	}
	public void setScore(float score) {
		this.score = score;
	}
}

比较器类

//先按成绩 降序 排序,如果成绩一样的话按id 升序 排序
class StudentComparator implements Comparator{

	/**
	  * return a negative integer, zero, or a positive integer as the first argument is less than, 
	  * 			equal to, or greater than the second. 
	  */
	@Override
	public int compare(Student s1, Student s2) {
		
		if(s1.getScore()>s2.getScore()){	//greater
			return -1;
		}else if(s1.getScore()==s2.getScore()){	//equals
			if(s1.getId()>s2.getId()){
				return 1;
			}else if(s1.getId()==s2.getId()){
				return 0;
			}else{
				return -1;
			}
		}else{	//less
			return 1;
		}
	}
}

Demo测试类

public class ListSortDemo {

	public static void main(String[] args) {
		
		Student s1 = new Student();
		s1.setId(10001);
		s1.setName("cat");
		s1.setScore(99.5f);
		
		Student s2 = new Student();
		s2.setId(10008);
		s2.setName("bba");
		s2.setScore(100.0f);
		
		Student s3 = new Student();
		s3.setId(10011);
		s3.setName("bac");
		s3.setScore(89.5f);
		
		Listlist = new ArrayList();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		
		System.out.println("排序之前-----------------------");
		for(Student stu:list){
			System.out.println("id="+stu.getId()+" name="+stu.getName()+" score="+stu.getScore());
		}
		
		Collections.sort(list, new StudentComparator());	//排序
		
		System.out.println("排序之后-----------------------");
		for(Student stu:list){
			System.out.println("id="+stu.getId()+" name="+stu.getName()+" score="+stu.getScore());
		}
	}
}

运行结果:
排序之前-----------------------
id=10001 name=cat score=99.5
id=10008 name=bba score=100.0
id=10011 name=bac score=89.5
排序之后-----------------------
id=10008 name=bba score=100.0
id=10001 name=cat score=99.5
id=10011 name=bac score=89.5

2. 通过实现Comparable接口来实现list的排序

假如现在我们有一个Person类的list集合,要让其按照一个Order属性进行排序,我们可以让Person类实现Comparable接口,重写其CompareTo方法即可,可以让程序按照我们想要的排列方式进行排序,如:这里我让Person按照order属性升序排序,具体实现如下:

Person实体类

public class Person implements Comparable{
    private String name;
    private Integer order;
 
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
 
    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /**
     * @return the order
     */
    public Integer getOrder() {
        return order;
    }
 
    /**
     * @param order
     *            the order to set
     */
    public void setOrder(Integer order) {
        this.order = order;
    }
 

    @Override
    public int compareTo(Person arg0) {
        return this.getOrder().compareTo(arg0.getOrder());      //这里定义你排序的规则。
    }
 }

测试类

public static void main(String[] args) {
    //初始化数据
    ListlistA = new ArrayList();
    Person p1 = new Person();
    Person p2 = new Person();
    Person p3 = new Person();
 
    p1.setName("name1");
    p1.setOrder(1);
    p2.setName("name2");
    p2.setOrder(2);
    p3.setName("name3");
    p3.setOrder(3);
 
    listA.add(p2);
    listA.add(p1);
    listA.add(p3);

    //排序
    Collections.sort(listA);

    //打印排序后的Person
    for (Person p : listA) {
        System.out.println(p.getName());
    }
}

结果:
name1
name2
name3

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。