java对List排序

java对List排序

偷懒用个插件

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

1.对象类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    //名称
    private String name;

    //年龄
    private Integer age;

    //身高
    private Double height;
}

2.测试类

import org.junit.jupiter.api.Test;

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

import static java.util.Comparator.comparing;

class JavaTestApplicationTests {

    @Test
    void contextLoads() {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("A1", 18, 1.76));
        studentList.add(new Student("B1", 16, 1.61));
        studentList.add(new Student("C1", 19, 1.82));
        studentList.add(new Student("D1", 17, 1.67));
        studentList.add(new Student("E1", 18, 1.67));

        // 排序前输出
        studentList.forEach(System.out::println);
        System.out.println("-------------------------------------------------------");
        // 按年龄排序
        // 降序
        studentList.sort(comparing(Student::getAge).reversed());
        studentList.forEach(System.out::println);
        System.out.println("-------------------------------------------------------");
        // 升序
        studentList.sort(comparing(Student::getAge));
        studentList.forEach(System.out::println);
        System.out.println("-------------------------------------------------------");
        // 按年龄排序,年龄相同再按身高排序
        // 降序
        studentList.sort(comparing(Student::getAge).thenComparing(Student::getHeight).reversed());
        studentList.forEach(System.out::println);
        System.out.println("-------------------------------------------------------");
        // 升序
        studentList.sort(comparing(Student::getAge).thenComparing(Student::getHeight));
        studentList.forEach(System.out::println);
        System.out.println("-------------------------------------------------------");
   }
    
    @Test
    void contextLoads2() {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("A1", 18, 1.76));
        studentList.add(new Student("B1", 16, 1.61));
        studentList.add(new Student("C1", 19, 1.82));
        studentList.add(new Student("D1", 17, 1.67));
        studentList.add(new Student("E1", 18, 1.67));


        List<Student> studentList2 = new ArrayList<>();
        studentList2.add(new Student("A2", 8, 1.76));
        studentList2.add(new Student("B2", 6, 1.61));
        studentList2.add(new Student("C2", 9, 1.82));
        studentList2.add(new Student("D2", 7, 1.67));
        studentList2.add(new Student("E2", 8, 1.67));

        List<Student> studentList3 = new ArrayList<>();
        studentList3.add(new Student("A3", 28, 1.76));
        studentList3.add(new Student("B3", 26, 1.61));
        studentList3.add(new Student("C3", 29, 1.82));
        studentList3.add(new Student("D3", 27, 1.67));
        studentList3.add(new Student("E3", 28, 1.67));

        List<Student> studentList4 = new ArrayList<>();
        studentList4.add(new Student("A4", 38, 1.76));
        studentList4.add(new Student("B4", 36, 1.61));
        studentList4.add(new Student("C4", 39, 1.82));
        studentList4.add(new Student("D4", 37, 1.67));
        studentList4.add(new Student("E4", 38, 1.67));

        List<Student> studentList5 = new ArrayList<>();
        studentList5.add(new Student("A5", 58, 1.76));
        studentList5.add(new Student("B5", 56, 1.61));
        studentList5.add(new Student("C5", 59, 1.82));
        studentList5.add(new Student("D5", 57, 1.67));
        studentList5.add(new Student("E5", 58, 1.67));

        List<Student> studentList6 = new ArrayList<>();
        studentList6.add(new Student("A6", 48, 1.76));
        studentList6.add(new Student("B6", 46, 1.61));
        studentList6.add(new Student("C6", 49, 1.82));
        studentList6.add(new Student("D6", 47, 1.67));
        studentList6.add(new Student("E6", 48, 1.67));

        List<List<Student>> lists = new ArrayList<>();

        lists.add(studentList);
        lists.add(studentList2);
        lists.add(studentList3);
        lists.add(studentList4);
        lists.add(studentList5);
        lists.add(studentList6);

        //排序前
        System.out.println("排序前");
        for (List<Student> list : lists) {
            System.out.println(list);
        }

        for (List<Student> list : lists) {
            //降序(大到小)
            list.sort(comparing(Student::getAge).reversed());
        }

        lists = lists.stream().sorted((o1, o2) -> {
            for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
                //大到小
                int c = o2.get(i).getAge().compareTo(o1.get(i).getAge());
                if (c != 0) {
                    return c;
                }
            }
            return Integer.compare(o1.size(), o2.size());
        }).collect(Collectors.toList());

        //排序后
        System.out.println("排序后");
        for (List<Student> list : lists) {
            System.out.println(list);
        }
    }
}
posted @ 2021-12-28 00:27  DarkH  阅读(49)  评论(0编辑  收藏  举报