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 ...
随机推荐
- 巧记 In/hasOwnProperty/for…in/for…of/forEach区别
写在前面 上面提到的这些东西,反复的记忆,反复的忘记.现分享一种巧记方法,有需要,请参考,希望你也过目不忘. 仅提供巧记思路.仅提供巧记思路.仅提供巧记思路. 1. in 用于判断属性是否在对象上(包 ...
- Jest单元测试进阶
Jest 命令行窗口中的指令 在学习Jest单元测试入门的时候,给Jest命令提供了一个参数 --watchAll, 让它监听测试文件或测试文件引入的文件的变化,从而时时进行测试.但这样做也带来一个问 ...
- 手表WACCHE单词WACCHE腕表
中文名:手表 外文名:watch,wacche 佩戴部位:手腕 拼音:shǒu biǎo 含义 1.戴在手腕上的小型计时器. 茅盾<夏夜一点钟>:“‘哼哼,这家伙!骗人的!’--她本能地校 ...
- 在Node.js中使用ejsexcel输出EXCEL文件
1.背景 在Nodejs应用程序中输出Excel,第一印象想到的一般是node-xlsx,这类插件不仅需要我们通过JS写入数据,还需要通过JS进行EXCEL显示样式的管理. 这是个大问题,不仅代码冗余 ...
- FreePascal - CodeTyphon 和 Lazarus, 如何像Delphi一样有代码之间的连线?
CodeTyphon 6.9 默认CodeTyphon没有开启代码之间连线功能,一直不知道如何调出来,在使用CodeTyphon 和 Lazarus 很渴望也能像Delphi那样有这种功能,实际上它们 ...
- Windows 10 下 GCC / LLVM 的安装和使用
win10下gcc的安装和makehttps://www.jianshu.com/p/46824c62dfed 方案1:直接官方下载安装minGw或Cygwininstall download: ht ...
- C/ C++ 快速上手
C++ 快速上手 (一)https://www.cnblogs.com/cosmo89929/archive/2012/12/22/2828745.html C++ 快速上手 (二)https://w ...
- 虚拟机安装xp系统教程
xp和win7安装过程有区别 xp.iso和win7.iso不一样 安装xp必须用特殊方法,此方法的大致步骤如下: 1.VMware新建xp类型的虚拟机 2.进入xp的PE系统 3.在PE系统分2个区 ...
- sql 使用条件
group by 1.在属性中出现相同内容的时候可以使用 2.也可以用来排序desc asc 批量插入数据 INSERT into 表(属性)(select *from 表) INSERT int ...
- 浅谈Python设计模式 - 原型模式
声明,本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 在<精通Python设计模式>中把设计模式分为三种类型: 创建型模式 ...