根据List集合中的对象属性排序

Posted 劈天造陆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据List集合中的对象属性排序相关的知识,希望对你有一定的参考价值。

首先创建一个Student对象,里面有三个属性,分别是int类型,String类型,Date类型

package com.sinoway.cisp.test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Student {

    private int age;
    private String name;
    private Date birthday;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + ", birthday="
                + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + "]";
        // 注意,为了方便看,这里将Date类型的birthday格式化为"yyyy-MM-dd"的String类型
    }

}

1、给int类型的age从小到大排序

package com.sinoway.cisp.test;

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

import org.junit.Test;

public class ObjectSortTest {
    
    @SuppressWarnings("all")
    public static void intSort(List<Student> studentList) {
        Collections.sort(studentList, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student student1 = (Student) o1;
                Student student2 = (Student) o2;
                if (student1.getAge() > student2.getAge()) {
                    return 1;
                } else if (student1.getAge() == student2.getAge()) {
                    return 0;
                } else {
                    return -1;
                }
            }
        });
    }

    @Test
    public void intSortTest() throws Exception {
        
        Student student1 = new Student();
        student1.setAge(18);
        student1.setName("C");
        student1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-03-20"));
        Student student2 = new Student();
        student2.setAge(16);
        student2.setName("B");
        student2.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-20"));
        Student student3 = new Student();
        student3.setAge(20);
        student3.setName("A");
        student3.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2018-02-20"));
        
        List<Student> studentList = new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        for (Student student : studentList) {
            System.out.println(student.toString());
        }
        System.err.println("----------");
        intSort(studentList);
        for (Student student : studentList) {
            System.out.println(student.toString());
        }
    }

}

输出为:

 

2、给String类型的name排序

@SuppressWarnings("all")
    public static void stringSort(List<Student> studentList) {
        Collections.sort(studentList, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student student1 = (Student) o1;
                Student student2 = (Student) o2;
                return student1.getName().compareTo(student2.getName());
            }
        });
    }

输出为:

3、给Date类型的birthday排序

@SuppressWarnings("all")
    public static void dateSort(List<Student> studentList) {
        Collections.sort(studentList, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student student1 = (Student) o1;
                Student student2 = (Student) o2;
                if (student1.getBirthday().getTime() > student2.getBirthday().getTime()) {
                    return 1;
                } else if (student1.getBirthday().getTime() == student2.getBirthday().getTime()) {
                    return 0;
                } else {
                    return -1;
                }
            }
        });
    }

输出为:

 

以上是关于根据List集合中的对象属性排序的主要内容,如果未能解决你的问题,请参考以下文章

List集合根据存储对象的属性字段排序实现

如何对List集合中的对象进行排序?

Java关于list集合根据集合元素对象的某个或多个属性进行排序的工具类

jdk1.7和jdk1.8对list集合根据对象的属性进行排序

如何对List集合中的对象进行按某个属性排序

List集合中元素排序