jave 对集合中的对象按照多个字段排序

不想你离开。 提交于 2019-12-10 08:46:17

格式一: public static <T extends Comparable<? super T>> void sort(List<T> list)

说明:该方法中的泛型<T>都是Comparable接口的子类,即只有是Comparable接口子类类型的数据,才能进行比较排序。如果其他类型的数据要进行比较排序,必须继承Comparable接口并

覆写equals()和compareTo()方法。其中如String类、Integer类都是Comparable接口子类,可以进行排序,而基本类型不能进行sort排序。比较项目在类内指定

package com.finance.common;

/**
 * Created by yg on 2017/12/6.
 */
public class Student implements Comparable<Student>{

    private String name;
    private String gender;
    private int id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int compareTo(Student arg0) { // 按照 name、id 字段从小到大排序,默认排序
        //String、Integer、Double、Float等类型都实现有compareTo方法
        if(this.name.compareTo(arg0.name) == 0) {
            return Integer.valueOf(id).compareTo(Integer.valueOf(arg0.id));
        }else{
            return this.name.compareTo(arg0.name);
        }
    }
}

调用如下代码:

package com.finance.common;

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

/**
 * Created by yg on 2017/12/6.
 */
public class ClientInterface {
    public static void main(String[] args) {
        Student songchao = new Student();
        songchao.setGender("Man");
        songchao.setId(100150);
        songchao.setName("SongChao");

        Student wuxing = new Student();
        wuxing.setGender("Man");
        wuxing.setId(100121);
        wuxing.setName("WuXing");

        Student songchao2 = new Student();
        songchao2.setGender("Women");
        songchao2.setId(100130);
        songchao2.setName("SongChao");

        List<Student> students = new ArrayList<Student>();
        students.add(songchao);
        students.add(wuxing);
        students.add(songchao2);
        for(Student student : students) {
            System.out.println("Name  " + student.getName() + " ID  " + student.getId());
        }

        System.out.println();
        Collections.sort(students); // student对象里面实现了 compare 方法
        for(Student student : students) {
            System.out.println("Name  " + student.getName() + " ID  " + student.getId());
        }
    }
}

格式二:public static <T> void sort(List<T> list, Comparator<? super T> c)

说明:该方法中指定比较方式Comparator<? super T> c,即c必须实现Comparator<? super T>接口,覆写compareTo()方法指定比较项目。比较项目在类外指定,比较灵活

1、在里面实现比较

Collections.sort(students, new Comparator<Student>() {

    @Override
    public int compare(Student arg0,Student arg1) {
        if(arg0.getName().compareTo(arg1.getName())== 0) {
            return Integer.valueOf(arg0.getId()).compareTo(Integer.valueOf(arg1.getId()));
        } else {
            return Integer.valueOf(arg0.getId()).compareTo(Integer.valueOf(arg1.getId()));
        }
    }
});

2、自定义比较器

import java.util.Comparator;
public class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student arg0,Student arg1) {
    if(arg0.getName().compareTo(arg1.getName())== 0) {
        return Integer.valueOf(arg0.getId()).compareTo(Integer.valueOf(arg1.getId()));
    } else {
        return Integer.valueOf(arg0.getId()).compareTo(Integer.valueOf(arg1.getId()));
    }
}

调用:

Collections.sort(studentList, new StudentComparator());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!