pojo对象的属性如下
private String id:
private String parentId:
private String name:
private int order
private String memoList<pojo> list 包含所有节点现在想用这个list进行排序, parentId为空是第一层,其余的为第二层,现在只想对第一层排序如何写这个代码?
private String id:
private String parentId:
private String name:
private int order
private String memoList<pojo> list 包含所有节点现在想用这个list进行排序, parentId为空是第一层,其余的为第二层,现在只想对第一层排序如何写这个代码?
select * from pojo where parentid is null order by parentid(这里也可以用其他的进行排序)
public class Pojo implements Comparable {
public int compareTo(Object o) {
if(this.parentId == null)
return 1;
return 0;
}
}//.....Collections.sort(list);
返回的节点包含全部数据(第一层和第二层)的。
Collections.sort(list, new Comparator<Pojo>() {
@Override
public int compare(Pojo p1, Pojo p2) {
// TODO Auto-generated method stub
if(p1.getParentId()!=null || p2.getParentId()!=null){
return -2;
}
int order1 = p1.getOrder();
int order2 = p2.getOrder();
if (order1 < order2) {
return -1;
} else {
return 1;
}
}我是这样写的,但是貌似只能降序,通过 order1 < order2 或者 order1>order2控制不了升序和降序。
});
code:import java.util.Iterator;
import java.util.List;
public class ComparableTest implements Comparable<List<Pojo>> { @Override
public int compareTo(List<Pojo> o) {
Iterator<Pojo> t = o.iterator();
int count = 0;
String[] praentIds = null;
while(t.hasNext()){
Pojo p = t.next();
praentIds[count] = p.getParentId();
count ++;
}
//下面的数组遍历,排序,可以写你自己的算法
for(int x=0;x<praentIds.length;x++){
for(int y=1;y<praentIds.length-1;y++){
if(){
}
}
}
return 0;
}
}