class MSComaparator implements Comparator<Calls>{
@Override
public int compare(Calls lhs, Calls rhs) {
// TODO Auto-generated method stub
return lhs.ms-rhs.ms;
}
}
class NameComaparator implements Comparator<Calls>{
@Override
public int compare(Calls lhs, Calls rhs) {
// TODO Auto-generated method stub
return lhs.name.comapreTo(rhs.name);
}
}
,並呼籲:
public void loadList(ArrayList<Calls> list) {
List<Calls> calls = new ArrayList<Calls>();
calls.addAll(list);
Collections.sort(calls,new MSComaparator())
}
,如果你只是想排序基於NS,你可以簡單的實現可比:
public class Calls implements Comparable<Calls>{
public long ms;
public name;
@Override
public int compareTo(Calls another) {
// TODO Auto-generated method stub
return this.ms>another.ms;
}
}
而且撥打:
public void loadList(ArrayList<Calls> list) {
List<Calls> calls = new ArrayList<Calls>();
calls.addAll(list);
Collections.sort(calls)
}
您嘗試過哪些示例?什麼不明確 –
比較器接口中有一種稱爲比較(對象O1,對象O2)的方法。你必須重寫(實現)它。把你的排序邏輯放在那裏......邏輯很簡單...如果O1.ms
TheLostMind
@TheLostMind +1完美地工作.. – ARP