通过jdk比较器Lambda表达式实现分组同时排序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
通过jdk比较器Lambda表达式实现分组同时排序
通过 jdk比较器 Lambda表达式实现分组同时排序
排序不通过数据库查询,通过jdk比较器Lambda表达式实现,效果如下:
//页面page对象 Pagination pagination = manager.getPage(queryType,cpn(pageNo), CookieUtils .getPageSize(request)); //crit.setProjection(Projections.groupProperty("parentId")); List<CmsDictionary> ls = (List<CmsDictionary>) pagination.getList(); /* jdk1.8前 Collections.sort(ls, new Comparator<CmsDictionary>() { @Override public int compare(CmsDictionary o1, CmsDictionary o2) { if(o1.getParentId().compareTo(o2.getParentId()) == 0) { return o1.getPriority().compareTo(o2.getPriority()); }else { return o1.getParentId().compareTo(o2.getParentId()); } } }); jdk1.8 ls.sort(new Comparator<CmsDictionary>() { @Override public int compare(CmsDictionary o1, CmsDictionary o2) { if(o1.getParentId().compareTo(o2.getParentId()) == 0) { return o1.getPriority().compareTo(o2.getPriority()); }else { return o1.getParentId().compareTo(o2.getParentId()); } } }); */ //jdk1.8 Lambda表达式 ls.sort(Comparator.comparing(CmsDictionary::getParentId).thenComparing(CmsDictionary::getPriority)); //排好序后再放入页面对象 pagination.setList(ls);
Βιβλιοθήκη Baidu
通过 jdk比较器 Lambda表达式实现分组同时排序
排序不通过数据库查询,通过jdk比较器Lambda表达式实现,效果如下:
//页面page对象 Pagination pagination = manager.getPage(queryType,cpn(pageNo), CookieUtils .getPageSize(request)); //crit.setProjection(Projections.groupProperty("parentId")); List<CmsDictionary> ls = (List<CmsDictionary>) pagination.getList(); /* jdk1.8前 Collections.sort(ls, new Comparator<CmsDictionary>() { @Override public int compare(CmsDictionary o1, CmsDictionary o2) { if(o1.getParentId().compareTo(o2.getParentId()) == 0) { return o1.getPriority().compareTo(o2.getPriority()); }else { return o1.getParentId().compareTo(o2.getParentId()); } } }); jdk1.8 ls.sort(new Comparator<CmsDictionary>() { @Override public int compare(CmsDictionary o1, CmsDictionary o2) { if(o1.getParentId().compareTo(o2.getParentId()) == 0) { return o1.getPriority().compareTo(o2.getPriority()); }else { return o1.getParentId().compareTo(o2.getParentId()); } } }); */ //jdk1.8 Lambda表达式 ls.sort(Comparator.comparing(CmsDictionary::getParentId).thenComparing(CmsDictionary::getPriority)); //排好序后再放入页面对象 pagination.setList(ls);
Βιβλιοθήκη Baidu