好吧假設我有一個如下所示的對象數組: obj(from,to) 我想通過比較from和to來對數組進行排序。什麼我希望做一個例子: 假設我有與這些參數 (0,2)(2,4)(0,3)(4,5)(2,3)
我要的對象是對象按照以下順序排序: (0,2)(0,3)(2,3)(2,4)(4,5)用數字對對數組排序
我想要比較前兩個「from」變量,並將較低的一個前面。如果它們相等,那麼我想要比較第二對數字。要做到這一點,我創建了一個比較方法
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 <EdgeI> {
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是另一個類中公開的列表。
你正在使用什麼語言和框架? – Neolisk
Java和你的框架是什麼意思? – ellangog
我不是Java專家,但是如果您說C#,則需要指定哪個.NET版本。 – Neolisk