日期:2014-05-20  浏览次数:20970 次

请教大家一个排序的问题
首先是一个Value类:
Java code
public class Value {
    public Value(int i, int j) {
        // TODO Auto-generated constructor stub
        id=i;
        parentid=j;
    }
    int id;
    int parentid;
}

然后是一个测试类:
Java code
public class OrderTest {
    public static void main(){
        List<Value> temp=new OrderTest().createList();
    }
    
    private List<Value> createList(){
        List<Value> temp=new ArrayList<Value>();
        temp.add(new Value(298,-1));
        temp.add(new Value(386,-1));
        temp.add(new Value(299,298));
        temp.add(new Value(401,400));
        temp.add(new Value(402,400));
        temp.add(new Value(403,400));
        temp.add(new Value(404,400));
        temp.add(new Value(400,-1));
        temp.add(new Value(405,400));
        temp.add(new Value(406,400));
        return temp;
    }
}

要对生成的temp列表进行排序,规则如下:
1.parentid如果为-1,那么代表他是一个父项,那么他的id就是他得名称,那么所有元素的parentid等于它的id都应该排在它的后面。所以最后生成的正确的列表的元素应该如下:
2.parentid从小到大排列
3.id从小到大排列
Java code

298,-1
299,298
386,-1
400,-1
401,400
402,400
403,400
404,400
405,400
406,400


在这里先谢谢各位了啊
分数不够,解决问题,一定双手奉上。

------解决方案--------------------
有个笨方法
先取出父节点
然后遍历
------解决方案--------------------
先遍历判断取出父节点然后替换新的List然后全部排列一次?。。。最直接的办法。。
------解决方案--------------------
典型的树形结构

需要迭代
------解决方案--------------------
Java code

package d20120208;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Value implements Comparable<Value>{
    public Value(int i, int j) {
        id = i;
        parentid = j;
    }

    int id;
    int parentid;
    @Override
    public int compareTo(Value o) {
        if(this.parentid==o.parentid){
            return this.id-o.id;
        }else{
            if(this.id==o.parentid){
                return -1;
            }else if(this.parentid==o.id){
                return 1;
            }else{
                return this.id-o.id;
            }
        }
    }
    
    public String toString(){
        return id+" "+parentid;
    }
    

}

------解决方案--------------------
Java code
ublic class Value implements Comparable<Value> {
    public Value(int i, int parentid) {
    this.id = i;
    this.parentid = parentid;
    }

    int id;
    int parentid;

    @Override
    public int compareTo(Value v) {
    return (this.id == v.id) ? (this.parentid < v.parentid ? -1
        : (this.parentid == v.parentid ? 0 : 1)) : (this.id < v.id ? -1
        : 1);
    }

public class OrderTest {
    public static void main(String[] args) {
    List<Value> temp = new OrderTest().createList();
    Collections.sort(temp);
    for (Value v : temp) {
        System.out.println(v.id + "----" + v.parentid);
    }
    }

    private List<Value> createList() {
    List<Value> temp = new ArrayList<Value>();
    temp.add(new Value(298, -1));
    temp.add(new Value(386, -1));
    temp.add(new Value(299, 298));
    temp.add(new Value(401, 400));
    temp.add(new Value(402, 400));
    temp.add(new Value(403, 400));
    temp.add(new Value(404, 400));
    temp.add(new Value(400, -1));
    temp.add(new Value(405, 400));
    temp.add(new Value(406, 400));
    return temp;
    }