首页 > 代码库 > java List的排序

java List的排序

List自定义排序

1、第一种方法,就是list中对象实现Comparable接口,重写compareTo接口, 对排序的字段进行比较。
2、第二种方法,就是在重载Collections.sort方法。

代码示例

package com.xmy.list.sort;

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

/**
 * description:list排序
 * Created with IDEA
 * User:xumingyang
 * Date:2016/10/14
 * Time:10:10
 */
public class ListMain {

    public static void main(String[] args) {
        comparePersonOrder();
        compareStudentOrder();
        compareStudentOrder2();
    }

    /**
     * 重载Collections.sort
     */
    public static void compareStudentOrder2(){
        List<Student> listA = new ArrayList<Student>();
        listA.add(new Student("name2", 2));
        listA.add(new Student("name1", 1));
        listA.add(new Student("name3", 3));

        Collections.sort(listA, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getOrder().compareTo(o2.getOrder());
            }
        });

        System.out.println("**********升序**********");
        for (Student s : listA) {
            System.out.println(s);
        }
    }

    /**
     * 重载Collections.sort
     */
    public static void compareStudentOrder(){
        List<Student> listA = new ArrayList<Student>();
        listA.add(new Student("name2", 2));
        listA.add(new Student("name1", 1));
        listA.add(new Student("name3", 3));

        System.out.println("**********升序**********");
        //升序
        Collections.sort(listA, new SortNum());
        for (Student s : listA) {
            System.out.println(s);
        }
        //降序
        System.out.println("**********降序**********");
        Comparator result = Collections.reverseOrder(new SortNum());
        Collections.sort(listA,result);
        for (Student s : listA) {
            System.out.println(s);
        }

    }

    /**
     * Person实现Comparable接口
     */
    public static void comparePersonOrder(){
        List<Person> listA = new ArrayList<Person>();
        listA.add(new Person("name2", 2));
        listA.add(new Person("name1", 1));
        listA.add(new Person("name3", 3));
        //升序
        Collections.sort(listA);
        System.out.println("**********升序**********");
        for (Person p : listA) {
            System.out.println(p);
        }
        //降序
        Collections.reverse(listA);
        System.out.println("**********降序**********");
        for (Person p : listA) {
            System.out.println(p);
        }
    }

}

/**
 * 比较器类
 */
class SortNum implements Comparator{
    @Override
    public int compare(Object o1, Object o2){
        Student s1 = (Student)o1;
        Student s2 = (Student)o2;
        return s1.getOrder() - s2.getOrder();
    }
}

class Student {

    private String name;
    private Integer order;

    public Student(String name, Integer order){
        this.name = name;
        this.order = 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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘\‘‘ +
                ", order=" + order +
                ‘}‘;
    }
}

class Person implements Comparable<Person> {

    private String name;
    private Integer order;

    @Override
    public String toString() {
        return "Person{" +
                "order=" + order +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }

    public Person(String name, Integer order){
        this.name = name;
        this.order = 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;
    }

    @Override
    public int compareTo(Person arg0) {
        return this.getOrder().compareTo(arg0.getOrder());
    }

}

对复合数据类型的数据的排序

package com.xmy.list.sort;

import java.util.Arrays;
import java.util.Comparator;

/**
 * description:
 * Created with IDEA
 * User:xumingyang
 * Date:2016/11/14
 * Time:15:55
 */
public class ArrayTest {

    public static void main(String[] args) {
        compareOrder();
    }

    /**
     * 排序
     */
    public static void compareOrder(){
        Point[] points = new Point[4];
        for(int i=0;i<4;i++)
            points[i]=new Point();
        //初始化数据
        points[0].order_1=2;points[0].order_2=1;
        points[1].order_1=2;points[1].order_2=2;
        points[2].order_1=1;points[2].order_2=2;
        points[3].order_1=0;points[3].order_2=1;
        //自定义排序
        Arrays.sort(points, new MyComprator());
        //输出排序结果
        for(int i=0;i<4;i++)
            System.out.println("排序后("+points[i].order_1+","+points[i].order_2+")");
    }

}

class Point{
    int order_1;
    int order_2;
}

/**
 * 比较器类
 */
class MyComprator implements Comparator {
    public int compare(Object arg0, Object arg1) {
        Point point_1 = (Point)arg0;
        Point point_2 = (Point)arg1;
        //优先按order_1进行升序排列
        if(point_1.order_1 != point_2.order_1)
            return point_1.order_1 > point_2.order_1 ? 1:-1;
        //再按order_2进行升序排列
        else
            return point_1.order_2 > point_2.order_2 ? 1:-1;
    }
}

  

List默认排序

    /**
     * 默认list排序方法
     */
    public static void sortList(){
        List<String> list = new ArrayList();
        list.add("赵");
        list.add("钱");
        list.add("孙");
        list.add("李");
        list.add("周");
        
        //注意:是根据的汉字的拼音的字母排序的,而不是根据汉字一般的排序方法
        System.out.println("**********升序**********");
        Collections.sort(list,Collator.getInstance(Locale.CHINA));
        for(String temp : list){
            System.out.println(temp);
        }
        System.out.println("**********降序**********");
        Collections.reverse(list);
        for(String temp : list){
            System.out.println(temp);
        }
    }

数组的排序

1、int[],double[],char[]等基数据类型的数组,Arrays类之只是提供了默认的升序排列

    /**
     * 默认数组排序方法
     */
    public static void sortArray(){
        String[] names = {"赵",  "钱", "孙", "李", "周"};
        System.out.println("排序前:" + Arrays.toString(names));
        //Arrays.sort(names);  排序无效
        Arrays.sort(names, Collator.getInstance(Locale.SIMPLIFIED_CHINESE));//升序;
        System.out.println(" 升序:" + Arrays.toString(names));

        Integer[] ages = {5,7,2,8,1};
        System.out.println("排序前:" + Arrays.toString(ages));
        Arrays.sort(ages);
        System.out.println(" 升序:" + Arrays.toString(ages));
    }

  

枚举类型的排序

对于枚举类型的enum1.compareTo(enum2)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大,跟值的字母先后顺序无关。

  

java List的排序