【Java】List排序

 

package com.crawler.boos.chrome.test;

import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:
 * @Date: 2022/7/7 20:30
 * @Description: 测试样例
 * @Version: v1.0
 */

@SpringBootTest
public class TestDemo extends AbstractTestNGSpringContextTests {
    /**
     * list排序(基本数据类型list)
     * 如:List<Integer> 类型的
     */
    @Test
    public void testListSort1() {
        List<Integer> integers = Arrays.asList(6, 3, 9, 1, 2);
        System.out.println(String.format("排序前:%s", integers)); // [6, 3, 9, 1, 2]

        // 1、正序排序(从小到大)
        integers.sort(null); // 默认正序排列(从小到大)
        System.out.println(String.format("排序后 默认正序排列(从小到大):%s", integers)); // [1, 2, 3, 6, 9]

        // 2、倒叙排序(从大到小)
        integers.sort((o1, o2) -> o2 - o1); // 这是lambda表达式简写的
        System.out.println(String.format("排序后 倒序排列(从大到小)简写方式:%s", integers)); // [9, 6, 3, 2, 1]
    }

    @Data
    class Student {
        private Integer age;
        private String name;
        private String birthday;

        Student(Integer age, String name, String birthday) {
            this.age = age;
            this.name = name;
            this.birthday = birthday;
        }
    }

    /**
     * list排序(对象类型list)
     * 如:ArrayList<Student> 类型的
     *
     * @throws ParseException
     */
    @Test
    public void testListSort2() throws ParseException {
        Student student1 = new Student(26, "26岁的人", "1995-01-24");
        Student student2 = new Student(38, "38岁的人", "1985-05-24");
        Student student3 = new Student(24, "24岁的人", "1996-03-20");
        Student student4 = new Student(20, "20岁的人", "2001-03-24");
        ArrayList<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
        System.out.println(JSONArray.toJSON(students)); // [{"birthday":"1995-01-24","name":"26岁的人","age":26},{"birthday":"1985-05-24","name":"38岁的人","age":38},{"birthday":"1996-03-20","name":"24岁的人","age":24},{"birthday":"2001-03-24","name":"20岁的人","age":20}]

        // 1、根据Student对象中的age属性 正序排序(从小到大)
        students.sort((stu1, stu2) -> stu1.age - stu2.age);
        System.out.println(JSONArray.toJSON(students)); // [{"birthday":"2001-03-24","name":"20岁的人","age":20},{"birthday":"1996-03-20","name":"24岁的人","age":24},{"birthday":"1995-01-24","name":"26岁的人","age":26},{"birthday":"1985-05-24","name":"38岁的人","age":38}]

        // 2、根据Student对象中的age属性 倒叙排序(从大到小)
        students.sort((stu1, stu2) -> stu2.age - stu1.age);
        System.out.println(JSONArray.toJSON(students)); // [{"birthday":"1985-05-24","name":"38岁的人","age":38},{"birthday":"1995-01-24","name":"26岁的人","age":26},{"birthday":"1996-03-20","name":"24岁的人","age":24},{"birthday":"2001-03-24","name":"20岁的人","age":20}]
    }
}

 

posted @ 2022-07-13 20:38  淡怀  阅读(77)  评论(0编辑  收藏  举报