对list某个条件排序,并实现分页

package com.jcloud.aioc.api.controller.Test;

import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.formula.functions.T;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Test {

    public static void main(String[] args) {
        List students = new ArrayList<>();

        students.add(new Student("张三","2017-07-01"));
        students.add(new Student("李四", "2018-07-02"));
        students.add(new Student("cde", "2018-06-01"));
        students.add(new Student("def", "2018-07-01"));
        students.add(new Student("efg", "2018-04-01"));
        //排序
       Collections.sort(students, new Comparator(){
            /*
             * int compare(Person p1, Person p2) 返回一个基本类型的整型,
             * 返回负数表示:p1 小于p2,
             * 返回0 表示:p1和p2相等,
             * 返回正数表示:p1大于p2
             */
            public int compare(Student p1, Student p2) {
                //按照Person的年龄进行升序排列
                if(getDate(p1.getCreateTime()) > getDate(p2.getCreateTime())){
                    return 1;
                }
                if(getDate(p1.getCreateTime())==getDate(p2.getCreateTime())){
                    return 0;
                }
                return -1;
            }
        }.reversed());
        for (Student student : students) {

            System.out.println(student.getName()+":"+student.getCreateTime());
        }
        PageableList page=new PageableList(0);
        PageAble page1 = page.page(students);
        System.out.println("分页后");
        List data = page1.getData();
        String s = JSON.toJSONString(page1);
        System.out.println(s);
    }
    public static Long getDate(String date){

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

  

package com.jcloud.aioc.api.controller.Test;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

public class PageableList {
    private int pageno;
    private int pageSize=2;
    private int sum;
    private int totalPage=0;

    public PageableList(int pageno) {
        this.pageno = pageno;
    }

    public PageAble page(List list){
        PageAble page=new PageAble();
        //分页
        Integer totalNum = list.size();
        //默认从零分页,这里要考虑这种情况,下面要计算。
        int pageNum = pageno + 1;
        if (totalNum > 0) {
            totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
        }
        if (pageNum > totalPage) {
            pageNum = totalPage;
        }
        int startPoint = (pageNum - 1) * pageSize;
        int endPoint = startPoint + pageSize;
        if (totalNum <= endPoint) {
            endPoint = totalNum;
        }
        list = list.subList(startPoint, endPoint);
        page.setCode("200");
        page.setPageno(pageno);
        page.setSum(pageNum);
        page.setTotalPage(totalPage);
        page.setData(list);

        return page;
    }
}

  

package com.jcloud.aioc.api.controller.Test;

public class Student {
    private String name;
    private String createTime;

    public Student(String name, String createTime) {
        this.name = name;
        this.createTime = createTime;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
}

  

package com.jcloud.aioc.api.controller.Test;

import org.apache.poi.ss.formula.functions.T;

import java.util.List;

public class PageAble {
    private String code;
    private int pageno;
    private int totalPage;
    private int sum;
    private List data;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getPageno() {
        return pageno;
    }

    public void setPageno(int pageno) {
        this.pageno = pageno;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }
}

  

//1.jdk8 lambda排序,带参数类型
orderList.sort(( Order ord1, Order ord2) -> ord2.getOrderId().compareTo(ord1.getOrderId()));

//2.jdk8 lambda排序,不带参数类型
orderList.sort(( ord1, ord2) -> ord2.getOrderId().compareTo(ord1.getOrderId()));

//3.jdk8 升序排序,Comparator提供的静态方法
Collections.sort(orderList, Comparator.comparing(Order::getOrderId));

//4.jdk8 降序排序,Comparator提供的静态方法
Collections.sort(orderList, Comparator.comparing(Order::getOrderId).reversed());

//5.jdk8 组合排序,Comparator提供的静态方法,先按orderId排序,orderId相同的按userId排序
Collections.sort(orderList, Comparator.comparing(Order::getOrderId).reversed().thenComparing(Order::getUserId));

orderList.stream().forEach(str -> System.out.println(str.getOrderId()+"/" + str.getUserId()));

 

参考:https://www.cnblogs.com/WAbby/p/9249833.html

转载于:https://www.cnblogs.com/e206842/p/11246586.html

你可能感兴趣的:(java,c#,数据库)