好吧,假设我有一个看起来像这样的对象数组:
obj(从,到)
我想通过比较从和到排序该数组.我想做的一个例子:
假设我有带有这些参数的对象
(0,2)(2,4)(0,3)(4,5)(2,3)
我希望对象按此顺序排序:
(0,2)(0,3)(2,3)(2,4)(4,5)
我想比较前两个“ from”变量,将下一个放在前面.如果它们相等,那么我想比较第二对数字.为此,我创建了一个compare方法
public int compare (EdgeI e1, EdgeI e2) {
if(e1.from < e2.from) { return -1; }
else if(e1.from == e2.from) {
if(e1.to < e2.to) { return -1; }
else if(e1.to == e2.to) { return 0; }
else if(e1.to > e2.to) { return 1; }
}
return 1;
}
这行得通吗?如果是这样,我将如何在数组中运行这种排序?
谢谢你的帮助.
编辑
public class mySorter implements Comparator {
public int compare(EdgeI e1, EdgeI e2) {
if(e1.from < e2.from) { return -1; }
else if(e1.from == e2.from) {
if(e1.to < e2.to) { return -1; }
else if(e1.to == e2.to) { return 0; }
else if(e1.to > e2.to) { return 1; }
}
return 1;
}
public void sorterM () {
Collections.sort(tet2, new mySorter());
}
}
我收到以下错误消息:集合无法解决,而tet2无法解决. Tet2是另一个类别中的List列表公众.