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);
    }
相关推荐
一枕槐安48720 分钟前
大模型应用后端设计:上下文管理、缓存、降级策略的工程方案总结
java·spring·缓存
T_Apollo20 分钟前
2.9 Java 25 版本新增特性和类
java
步行cgn35 分钟前
@RequestMapping 注解详解
java·后端·spring
鶴哥只手遮天36 分钟前
Spring Boot 4.1.1 深度源码解析-第 15 章 · 条件注解体系:全部 `@ConditionalOnXxx` 的实现原理
java·后端·开源
鶴哥只手遮天37 分钟前
Spring Boot 4.1.1 深度源码解析-第 30 章 · 基础设施能力:SSL Bundle、Task 与虚拟线程、Retry
java·后端·开源
IT研究室38 分钟前
最新计算机毕业设计选题推荐-基于微信小程序的汉服交易系统-网站-文档指导-Java-springboot
java·微信小程序·课程设计
鶴哥只手遮天42 分钟前
Spring Boot 4.1.1 深度源码解析-第 26 章 · 指标、观测与链路追踪:Micrometer 与 OpenTelemetry
java·后端·开源
Wang's Blog44 分钟前
Java 项目实战: 交友项目 - 章节复盘:微服务骨架与手机号验证码登录
java·微服务·交友
data analyse 4561 小时前
Session数怎么计算?明确会话切分、超时与跨页规则
java·服务器·数据库
鶴哥只手遮天1 小时前
Spring Boot 4.1.1 深度源码解析-第 19 章 · Spring MVC 自动配置:DispatcherServlet、消息转换
java·后端·开源