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 ...
随机推荐
- jQuery基础学习
一.简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是“Write ...
- jquery问题,如何调用带this的函数?
这样写: 1 2 3 4 5 6 7 8 9 10 11 12 $(".aa").on("mouseout",function(){ var obj = ...
- DataPipeline数据融合重磅功能丨一对多实时分发、批量读取模式
为能更好地服务用户,DataPipeline最新版本支持: 1. 一个数据源数据同时分发(实时或定时)到多个目的地: 2. 提升Hive的使用场景: 写入Hive目的地时,支持选择任意目标表字段作为 ...
- MYSQL中的时间类型
时间上总共有五中表示方法:它们分别是 time.date.datetime.timestamp和year. time : “hh:mm:ss”格式表示的时间值,格式显示TIME值,但允许使用字符串或 ...
- Android 创建工程
安卓系统占有率 创建 Application name:程序的名称,一般会出现在应用程序的标题栏 Project name:一个项目的名称,实际对应一个文件夹 Pakcage name:此名理论上可以 ...
- TP5.0使用助手函数model出现\common\Model\类不存在
在ThinkPHP5.0中有一个助手助手函数model(),可以实例化具体的模型,包括分层模型,只要传入类名(第一个参数),分层名(第二个参数).这个函数其实是ThinkPHP框架Loader中的一个 ...
- 深入理解 ValueTask
深入理解 ValueTask .NET Framework 4 里面的命名空间为 System.Threading.Tasks的 Task 类.这个类以及它派生的 Task<TResult> ...
- pandas 之 字符串处理
import numpy as np import pandas as pd Python has long been a popular raw data manipulation language ...
- mysql float和decimal
结论: 1. float 默认只保存6位(除去小数点),如果超过6位,则四舍五入,所以float存储的数据是不精确的,只是近似值: 2. decimal,如果输入的数据超过了定义的最大值,那么则溢出, ...
- git使用.gitignore文件忽略相关文件上传
在使用git时,有些文件是不需要上传到仓库中的,比如idea/eclipse相关的文件,编译后的文件(target目录)等. 可以使用.gitignore文件进行配置.在git本地仓库创建.gitig ...