java分页与排序

java分页与排序

代码 如下不解释

public class SortOb implements Comparable{

private String  name;
private int age;
private Date createDate;

public SortOb(String name,int age,Date createDate) {
	this.name = name;
	this.age = age;
	this.createDate = createDate;
}

public Date getCreateDate() {
	return createDate;
}

public void setCreateDate(Date createDate) {
	this.createDate = createDate;
}

public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}

// @Override
// public int compareTo(SortOb o) {
// return this.age - o.getAge();
// }
@Override
public int compareTo(SortOb o) {
try {
Date dt1 = o.getCreateDate();
Date dt2 = this.getCreateDate();
if (dt1.getTime() > dt2.getTime()) {
return -1;//小的放前面
}else {
return 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}

public class ListSortTest {

public static void main(String[] args) throws ParseException {
	List<SortOb> list = new ArrayList<SortOb>();
	DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	Date dateA = format1.parse("2002-1-18 22:01:01");
	Date dateB = format1.parse("2001-1-18 22:01:01");
	Date dateC = format1.parse("2005-1-18 22:01:01");
	Date dateD = format1.parse("2004-1-18 22:01:01");
	Date dateE = format1.parse("2006-1-18 22:01:01");
	Date dateF = format1.parse("2003-1-18 22:01:01");
	Date dateG = format1.parse("2007-1-18 22:01:01");
	Date dateH = format1.parse("2009-1-18 22:01:01");
	Date dateI = format1.parse("2006-1-18 22:01:01");
	Date dateJ = format1.parse("2008-1-18 22:01:01");
	Date dateK = format1.parse("2010-1-18 22:01:01");
	Date dateL = format1.parse("2009-1-18 22:01:01");

	SortOb a = new SortOb("bb", 2,dateA);
	SortOb b = new SortOb("aa", 1,dateB);
	SortOb c = new SortOb("ee", 5,dateC);
	SortOb d = new SortOb("dd", 4,dateD);
	SortOb e = new SortOb("ff", 6,dateE);
	SortOb f = new SortOb("cc", 3,dateF);
	SortOb g = new SortOb("gg", 7,dateG);
	SortOb h = new SortOb("ii", 9,dateH);
	SortOb i = new SortOb("zz", 6,dateI);
	SortOb j = new SortOb("hh", 8,dateJ);
	SortOb k = new SortOb("hh", 10,dateK);
	SortOb l = new SortOb("jj", 9,dateL);
	list.add(a);
	list.add(b);
	list.add(c);
	list.add(d);
	list.add(e);
	list.add(f);
	list.add(g);
	list.add(h);
	list.add(i);
	list.add(j);
	list.add(k);
	list.add(l);
	Collections.sort(list, (SortOb b1, SortOb b2) -> b2.compareTo(b1));

	Map<String, Object> map = getPagingResultMap(list,2,10);
	List<SortOb> pageList = (List<SortOb>) map.get("result");

	for (SortOb ob : pageList) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(ob.getCreateDate());
		System.out.println(dateString);
	}

// for (SortOb ob : list) {
// System.out.println(ob.getAge());
// }

}

public static  Map<String, Object> getPagingResultMap(List<SortOb> list, Integer currPageNo, Integer pageSize) {
	Map<String, Object> retMap = new HashMap<>();

	if (list.isEmpty()) {
		retMap.put("result", Collections.emptyList());
		retMap.put("totalPageNum", 0);
		return retMap;
	}
	
	int totalRowNum = list.size();
	
	//页数超过数据总页数返回空list
	if (totalRowNum< pageSize * (currPageNo-1)) {
		retMap.put("result", Collections.emptyList());
		retMap.put("totalPageNum", totalRowNum);
		return retMap;
	}
	
	int totalPageNum = (totalRowNum - 1) / pageSize + 1;

	int realPageNo = currPageNo;
	if (currPageNo > totalPageNum) {
		realPageNo = totalPageNum;
	} else if (currPageNo < 1) {
		realPageNo = 1;
	}

	int fromIdx = (realPageNo - 1) * pageSize;
	int toIdx = realPageNo * pageSize > totalRowNum ? totalRowNum : realPageNo * pageSize;
			
	List<SortOb> result = list.subList(fromIdx, toIdx);
	
	retMap.put("result", result);
	retMap.put("totalPageNum", totalPageNum);
	return retMap;

}

// public static void main(String[] args) {
// int totalRowNum = 12;
// int pageSize = 10;
// int currPageNo = 2;
// System.out.println((totalRowNum< pageSize * (currPageNo-1)));
// }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值