Java将list<map>或者list<entity>集合根据指定字段排序
今天项目中用到了,特记录一下
一. List<Map>
如果 item.get(sortField) 有时间,有数字的时候直接toString(),数组结果的排序结果可能不正确
List<Map<String, Object>> resourceCatalogFrontVoList = resourceMapper.selectPublishResourceCatalogList(keyWord, sysId, shareType, openAttribute,
classifyId, resourceType, page);
//排序
Comparator<Map<String, Object>> comparator = Comparator.comparing(item -> item.get(sortField).toString());
if (FrontConstant.SORT_TYPE_ASC == sortType){
//升序
resourceCatalogFrontVoList.sort(comparator);
}else {
resourceCatalogFrontVoList.sort(comparator.reversed());
}
//分页
return page.setRecords(resourceCatalogFrontVoList);
补充
if (FrontConstant.SORT_TYPE_ASC == sortType){
//升序
Collections.sort(resourceCatalogFrontVoList, Comparator.comparing(
item -> item.get(sortField).toString()));
}else {
Collections.sort(resourceCatalogFrontVoList, Comparator.comparing(
item -> item.get(sortField).toString(), Comparator.reverseOrder()));
}
二.List<entity>
List<ResMessageVo> messageVoList = messageMapper.selectUnReadMessage(null, null, null, MessageConstant.MESSAGE_TYPE_MOUNT_RESOURCE_RESULT); //排序
Comparator<ResMessageVo> comparator = Comparator.comparing(item -> item.getCreateTime());
if (FrontConstant.SORT_TYPE_ASC == sortType){
//升序
messageVoList.sort(comparator);
}else {
messageVoList.sort(comparator.reversed());
}
Java将list<map>或者list<entity>集合根据指定字段排序的更多相关文章
- 利用Entity Framework修改指定字段中的值
利用Entity Framework修改指定字段中的值一般我们编辑某些模型的时候会用到类似这样的代码: [HttpPost] public ActionResult Edit(Article mode ...
- list集合中指定字段去重
在开发中,有时会需要指定字段去重,以下为实现方法: 假设有个房地产权的类,其中宗地代码ZDDM值重复,而我们在前端页面显示时,只需要一条数据,因为公共字段都一样: IEqualityComparer需 ...
- entity framwork修改指定字段
1.ef修改时指修改指定字段public void ChangePassword(int userId, string password) { var user = new User() { Id = ...
- Entity Framework搜索指定字段解决方案
public class Book { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; ...
- List集合基于某个字段排序
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...
- List集合对象根据字段排序
//把需要比较的对象实现Comparable接口实现compareTo方法 public class Address implements Comparable<Address> { St ...
- Linq to entity 执行多个字段排序的方法
可以连续使用:OrderBy,ThenBy 或者 OrderByDescending,ThenByDescending var data = db.User .Where(u => u.User ...
- java的list集合如何根据对象中的某个字段排序?
转自:http://blog.csdn.net/wangjuan_01/article/details/51351633 List集合按某个字段排序 package wjtest_01; import ...
- 黑马程序员——JAVA基础之Map集合
------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...
随机推荐
- 函数内this指向+排序+找出数组大小项+Math类
解决函数内this指向: 1,可以在函数外提前声明变量 _this/that = this 2,通过apply()和call()来修改函数内的this指向 二者区别: 用法是一样的,参数形式不一样 f ...
- 99.9%的Java程序员都说不清的问题:JVM中的对象内存布局?
本文转载自公众号:石彬的架构笔记,阅读大约需要8分钟. 作者:李瑞杰 目前就职于阿里巴巴,资深 JVM 研究人员 在 Java 程序中,我们拥有多种新建对象的方式.除了最为常见的 new 语句之外,我 ...
- Golang Web应用 创建docker镜像笔记(win 平台)
记录的是 本地编译好了再创建容器镜像的方法 ,这样子生成的镜像文件比较小,方便分发部署 win 平台需要设置golang交叉编译 生成linux可执行文件 CMD下: Set GOOS="l ...
- 设计模式之JDK动态代理源码分析
这里查看JDK1.8.0_65的源码,通过debug学习JDK动态代理的实现原理 大概流程 1.为接口创建代理类的字节码文件 2.使用ClassLoader将字节码文件加载到JVM 3.创建代理类实例 ...
- Linux操作系统内核编译之NTFS文件系统模块支持案例
Linux操作系统内核编译之NTFS文件系统模块支持案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内核编译概述 单内核体系设计.但充分借鉴了微内核设计体系的优点,为内核引 ...
- The 2016 ACM-ICPC Asia China-Final D. Ice Cream Tower 二分 + 贪心
题目大意: 对于给出的n个冰激凌球的大小,满足下面的球的大小是上一个的至少2倍,对于给出的k(由k的冰激凌球才能算作一个冰激凌塔),问n个冰激凌球可以最多堆出多少个高度为k的冰激凌塔 题目分析: 对于 ...
- js 正则表达式 贪婪与惰性
首先引入一个介绍比较详细的网站 http://www.jb51.net/article/31491.htm 接下来是本人的简介 其实贪婪和惰性很容易理解,从字面意思我们就可以知道,所谓的"贪 ...
- POJ 3322 Bloxorz
#include<cstring> #include<algorithm> #include<iostream> #include<cstdio> #i ...
- 关于background-image设置背景图片
每天进步一小步,一年进步一大步. 本篇主要介绍背景图片设置,平铺,x y方向上的平铺,是否重复显示no repeat 显示的初始位置 background-image:url(images/inde ...
- oracle中删除表:drop、delete、truncate
相同点,使用drop delete truncate 都会删除表中的内容 drop table 表名 delete from 表名(后面不跟where语句,则删除表中所有的数据) truncate t ...