Java list中实体类的按照某个字段大小排序

Java list中实体类的按照某个字段大小排序

在Java中,可以使用Collections.sort()方法结合自定义的比较器来对List中的实体类按照某个字段进行排序。以下是一个示例代码,演示了如何根据实体类中的age字段对List进行排序:
go 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
class Person {
    String name;
    int age;
 
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    // getters and setters
    public String getName() {
        return name;
    }
 
    public int getAge() {
        return age;
    }
}
 
public class SortExample {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 30));
        people.add(new Person("Bob", 25));
        people.add(new Person("Charlie", 35));
 
        // 根据年龄排序
        Collections.sort(people, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return Integer.compare(o1.getAge(), o2.getAge());
            }
        });
 
        // 打印排序结果
        for (Person p : people) {
            System.out.println(p.getName() + ": " + p.getAge());
        }
    }
}

在这个例子中,我们定义了一个Person类,并创建了一个包含三个Person对象的List。使用Collections.sort()方法和一个匿名内部类来实现Comparator接口,以便根据age字段对List进行排序。结果将按年龄升序排列。如果你需要按降序排序,可以使用Integer.compare(o2.getAge(), o1.getAge())。

例子

go 复制代码
  @GetMapping("/selectJobNumByMoth")
    @Anonymous
    public AjaxResult selectJobNumByMoth(){
        //ArrayList<PlatMonthNumDto> list = new ArrayList<>();
        //后台查出来的月份数据
        List<PlatMonthNumDto> platMonthNumDtos = platEmploymentJobService.selectJobNumByMoth();
        for (int i = 1; i < 13; i++) {
            String str = String.valueOf(i);
            //判断12个月份是否有数据,如果没有,则添加上
            boolean b1 = platMonthNumDtos.stream()
                    .map(PlatMonthNumDto::getMonth)
                    .anyMatch(a -> a.toString().contains(str));
            if (!b1){
                PlatMonthNumDto platMonthNumDtoAdd = new PlatMonthNumDto();
                platMonthNumDtoAdd.setMonth(i);
                platMonthNumDtoAdd.setDataCount(0);
                platMonthNumDtos.add(platMonthNumDtoAdd);
            }
        }
        //按照 某个字段进行排序
        Collections.sort(platMonthNumDtos, new Comparator<PlatMonthNumDto>() {
            @Override
            public int compare(PlatMonthNumDto o1, PlatMonthNumDto o2) {
                return Integer.compare(o1.getMonth(), o2.getMonth());
            }
        });
        return AjaxResult.success(platMonthNumDtos);
    }
相关推荐
雾月5515 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
24k小善1 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者1 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
猿周LV2 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
晨集2 小时前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同
时间之城2 小时前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
椰羊~王小美2 小时前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言
凯酱2 小时前
MyBatis-Plus分页插件的使用
java·tomcat·mybatis
程序员总部2 小时前
如何在IDEA中高效使用Test注解进行单元测试?
java·单元测试·intellij-idea