List 排序

用Collections.sort方法对list排序

基本数据类型排序

package com.heima.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BaseListSort {
	public static void main(String[] args) {
		// 创建list
	    List<Integer> list = new ArrayList<Integer>();
	    // 插入元素
	    list.add(2);
	    list.add(0);
	    list.add(3);
	    list.add(4);
	    list.add(1);
	    Collections.sort(list);
	    for (int i : list) {
	        System.out.println(i);
	    }
	}
}

由于基础数据类型的包装类都已经实现了Comparable接口,所以可以直接调用Collections的sort方法。



实体类排序

第一种是list中的对象实现Comparable接口,如下:

/**
* 根据order对User排序
*/
public class User implements Comparable{
    private String name;
    private Integer order;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getOrder() {
        return order;
    }
    public void setOrder(Integer order) {
        this.order = order;
    }
    public int compareTo(User arg0) {
        return this.getOrder().compareTo(arg0.getOrder());
    }
}

测试:

public class Test{
 
    public static void main(String[] args) {
        User user1 = new User();
        user1.setName("a");
        user1.setOrder(1);
        User user2 = new User();
        user2.setName("b");
        user2.setOrder(2);
        List list = new ArrayList();
        //此处add user2再add user1
        list.add(user2);
        list.add(user1);
        Collections.sort(list);
        for(User u : list){
            System.out.println(u.getName());
        }
    }

第二种方法是根据Collections.sort重载方法来实现,例如:


/**
* 根据order对User排序
*/
public class User { //此处无需实现Comparable接口
    private String name;
    private Integer order;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getOrder() {
        return order;
    }
    public void setOrder(Integer order) {
        this.order = order;
    }
}

public class Test{
    public static void main(String[] args) {
        User user1 = new User();
        user1.setName("a");
        user1.setOrder(1);
        User user2 = new User();
        user2.setName("b");
        user2.setOrder(2);
        List list = new ArrayList();
        list.add(user2);
        list.add(user1);
         
        Collections.sort(list,new Comparator(){
            public int compare(User arg0, User arg1) {
                return arg0.getOrder().compareTo(arg1.getOrder());
            }
        });
        for(User u : list){
            System.out.println(u.getName());
        }
    }
}

多字段的场合:

Collections.sort(list,new Comparator(){
            public int compare(User arg0, User arg1) {
 //            第一次比较专业
                int i = arg0.getOrder().compareTo(arg1.getOrder());
 
//            如果专业相同则进行第二次比较
            if(i==0){
//                第二次比较
                int j=arg0.getXXX().compareTo(arg1.getXXX());
//                如果学制相同则返回按年龄排序
                if(j==0){
                    return arg0.getCCC().compareTo(arg1.getCCC());
                }
                return j;
            }
            return i;
            }
        });


中文字符串排序

package com.heima.collection;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;

public class ChineseCharSort {
	public static void main(String[] args) {
		ArrayList<String> list=new ArrayList<String>();
		list.add("中国");
		list.add("美国");
		list.add("俄罗斯");
		Comparator<Object> comparator=Collator.getInstance(Locale.ENGLISH);
		Collections.sort(list,comparator);
		System.out.println(list);
	}
	
	
}


你可能感兴趣的:(List 排序)