其他分享
首页 > 其他分享> > 虚拟字段进行排序后分页

虚拟字段进行排序后分页

作者:互联网

遇到排序后分页是,会在sql中继续宁排序,用PageHelper进行分页。但如果是java代码里的虚拟字段排序,程序的分页自然在排序之前,排序时只是对这一页(limit)进行排序,并不完整,因此,需要自己写一个简单的list分页
 1     public static List startPage(List list, Integer pageNum, Integer pageSize) {
 2         if (list == null) {
 3             return null;
 4         }
 5         if (list.size() == 0) {
 6             return null;
 7         }
 8         Integer count = list.size(); // 记录总数
 9         Integer pageCount = 0; // 页数
10         if (count % pageSize == 0) {
11             pageCount = count / pageSize;
12         } else {
13             pageCount = count / pageSize + 1;
14         }
15         int fromIndex = 0;
16         int toIndex = 0;
17         if (pageNum != pageCount) {
18             fromIndex = (pageNum - 1) * pageSize;
19             toIndex = fromIndex + pageSize;
20         } else {
21             fromIndex = (pageNum - 1) * pageSize;
22             toIndex = count;
23         }
24         List pageList = list.subList(fromIndex, toIndex);
25         return pageList;
26     }

使用

        resultsList = OtherUtils.startPage(resultsList,pageNum,pageSize);

        JSONObject resultObject = new JSONObject();
        resultObject.put("content",resultsList);
        resultObject.put("pageNum",pageNum);
        resultObject.put("pageSize",pageSize);
        Double totalPages = Double.valueOf(resultsList.size())/Double.valueOf(pageSize);
        resultObject.put("totalPages",(int)Math.ceil(totalPages));
        resultObject.put("totalSize",resultsList.size());
View Code

 

标签:pageNum,分页,pageSize,list,fromIndex,resultObject,虚拟,排序
来源: https://www.cnblogs.com/lgqrlchinese/p/15303523.html