首先 pojo里需要 Comparator接口,实现 int compare(Object o1, Object o2)方法
@Override
public int compare(Object o1, Object o2)
{
ThemePojo themePojo1= (ThemePojo)o1;
ThemePojo themePojo2= (ThemePojo)o1;
if(themePojo1.getId() > themePojo2.getId())
{
return 1;
}
return 0;
}
然后在具体需要排序的地方增加如下代码
// 对主题内容进行排序
Comparator<ThemePojo> comparator = new Comparator<ThemePojo>() {
public int compare(ThemePojo t1, ThemePojo t2) {
// 先排id
if (t1.getId() != t2.getId()) {
return t1.getId() - t2.getId();
}else
{
t1.getCreateTime().compareTo(t2.getCreateTime());
}
return t1.getId() - t2.getId();
}
};
Collections.sort(listPojo, comparator);
Timestamp createTime = null;
for(ThemePojo themePojo:listPojo)
{
createTime = themePojo.getCreateTime();
}