日期:2014-05-20 浏览次数:21286 次
public class Value {
public Value(int i, int j) {
// TODO Auto-generated constructor stub
id=i;
parentid=j;
}
int id;
int parentid;
}
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;
}
}
298,-1 299,298 386,-1 400,-1 401,400 402,400 403,400 404,400 405,400 406,400
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;
}
}
------解决方案--------------------
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;
}