java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java中List对象集合排序

Java中List对象集合按对象中某字段进行排序举例

作者:壹猿

这篇文章主要给大家介绍了关于Java中List对象集合按对象中某字段进行排序的相关资料,我们在日常开发中也经常会用到排序算法,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

在Java中,可以使用List集合对象的sort(Comparator<? super E> c)方法,按照对象中某字段对List集合进行排序

default void sort(Comparator<? super E> c) {
    Object[] a = this.toArray();
    Arrays.sort(a, (Comparator) c);
    ListIterator<E> i = this.listIterator();
    for (Object e : a) {
        i.next();
        i.set((E) e);
    }
}

栗子:

import java.util.*;
import java.util.stream.Collectors;
public class StreamTest {
    public static void main(String[] args) {
        List<Person2> personList = new ArrayList<Person2>();
        personList.add(new Person2("Tom", 8900, 18,"male", "A"));
        personList.add(new Person2("Jack", 7000, 19,"male", "A"));
        personList.add(new Person2("Lily", 7800, 20,"female", "D"));
        personList.add(new Person2("Anni", 8200, 18,"female", "B"));
        personList.add(new Person2("Owen", 9500, 23,"male", "D"));
        personList.add(new Person2("Alisa", 7900, 28,"female", "C"));
        //根据List集合对象的sort方法(语句后x.get与y.get调换位置即为倒序)
        personList.sort((x,y) -> Integer.compare(x.getSalary(),y.getSalary()));//int类型正序
        personList.forEach(System.out::println);
        System.out.println("================================");
        personList.sort((a, b) -> a.getSalary() - b.getSalary());//int类型正序
        personList.forEach(System.out::println);
        System.out.println("================================");
        personList.sort(Comparator.comparing(Person2::getSalary));//int类型正序排序
        personList.forEach(System.out::println);
        System.out.println("================================");
        personList.sort((a,b) -> a.getLevel().compareTo(b.getLevel()));//其他类型正序
        personList.forEach(System.out::println);
        System.out.println("================================");
        //stream流的sorted方法正序排序(倒序:Comparator.comparing(Person2::getSalary).reversed())
        personList = personList.stream().sorted(Comparator.comparing(Person2::getSalary)).collect(Collectors.toList());
        personList.forEach(System.out::println);
        System.out.println("================================");
    }
}
class Person2 {
    private String name;  // 姓名
    private int salary; // 薪资
    private int age; // 年龄
    private String sex; //性别
    private String level;  // 级别
    // 构造方法
    public Person2(String name, int salary, int age, String sex, String level) {
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.sex = sex;
        this.level = level;
    }
    // 省略了get和set,请自行添加
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    @Override
    public String toString() {
        return "Person2{" +
                "name=" + name +
                ", salary='" + salary + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", level=" + level +
                '}';
    }
}

打印输出结果如下:

Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================
Person2{name=Jack, salary='7000', age='19', sex='male', level=A}
Person2{name=Lily, salary='7800', age='20', sex='female', level=D}
Person2{name=Alisa, salary='7900', age='28', sex='female', level=C}
Person2{name=Anni, salary='8200', age='18', sex='female', level=B}
Person2{name=Tom, salary='8900', age='18', sex='male', level=A}
Person2{name=Owen, salary='9500', age='23', sex='male', level=D}
================================

总结

到此这篇关于Java中List对象集合按对象中某字段进行排序的文章就介绍到这了,更多相关Java中List对象集合排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文