java排序

zyl2001 2012-09-04 11:12:01
pojo对象的属性如下
private String id:
private String parentId:
private String name:
private int order
private String memo

List<pojo> list 包含所有节点

现在想用这个list进行排序, parentId为空是第一层,其余的为第二层,现在只想对第一层排序

如何写这个代码?
...全文
158 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
balabala_sean 2012-09-04
  • 打赏
  • 举报
回复
大概的思路
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;
}
}
安特矮油 2012-09-04
  • 打赏
  • 举报
回复
实现一个比较器就可以了,
zyl2001 2012-09-04
  • 打赏
  • 举报
回复
List<pojo> 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控制不了升序和降序。
});
艳沐石 2012-09-04
  • 打赏
  • 举报
回复
LS的已经实现了,java类排序和数据库排序了!

LZ想要用什么技术排序呢?
cscript 2012-09-04
  • 打赏
  • 举报
回复

public class Pojo implements Comparable {

public int compareTo(Object o) {
if(this.parentId == null)
return 1;
return 0;
}
}

//.....

Collections.sort(list);
wujunlongsy4 2012-09-04
  • 打赏
  • 举报
回复
select * from pojo where parentid is null order by order(这里不能用order作字段名称)
wujunlongsy4 2012-09-04
  • 打赏
  • 举报
回复
你的第一层parentid为空,要对第一层排序
select * from pojo where parentid is null order by parentid(这里也可以用其他的进行排序)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧