2013-09-28 32 views
2

我想使用Collections.sort()對java中的對象列表進行排序。不過,我不斷收到此錯誤:類型參數不中其綁定」有誰知道我怎麼能解決這個問題使用Collections.sort()對對象排序。獲取一個錯誤

我的代碼

public List<String> fetchNumbersForLeastContacted() 
    { 


    List<String> phonenumberList = getUniquePhonenumbers(); 
    List<TopTen> SortList = new ArrayList<TopTen>(); 


    Date now = new Date(); 
    Long milliSeconds = now.getTime(); 

    //Find phone numbers for least contacted 
    for (String phonenumber : phonenumberList) 
    { 



     int outgoingSMS = fetchSMSLogsForPersonToDate(phonenumber, milliSeconds).getOutgoing(); 
     int outgoingCall = fetchCallLogsForPersonToDate(phonenumber, milliSeconds).getOutgoing(); 

     //Calculating the total communication for each phone number 
     int totalCommunication = outgoingCall + outgoingSMS; 

     android.util.Log.i("Datamodel", Integer.toString(totalCommunication)); 

     SortList.add(new TopTen(phonenumber, totalCommunication, 0)); 

    } 

    //This is where I get the error 
    Collections.sort(SortList); 

的TopTen.class

public class TopTen { 

private String phonenumber; 
private int outgoing; 
private int incoming; 


public TopTen (String phonenumber, int outgoing, int incoming) 
{ 
    this.phonenumber = phonenumber; 
    this.incoming = incoming; 
    this.outgoing = outgoing; 


} 

public String getPhonenumber() { 
    return phonenumber; 
} 

public void setPhonenumber(String phonenumber) { 
    this.phonenumber = phonenumber; 
} 

public int getOutgoing() { 
    return outgoing; 
} 

public void setOutgoing(int outgoing) { 
    this.outgoing = outgoing; 
} 

public int getIncoming() { 
    return incoming; 
} 

public void setIncoming(int incoming) { 
    this.incoming = incoming; 
}} 

回答

2
public static void sort (List<T> list) 

此方法僅可用於如果T inplements的Comparable接口。 implements Comparable意味着存在一個標準,通過該標準,可以比較和排序類型爲T的兩個對象。在你的情況下,TTopTen,它不實現Comparable

你需要做什麼:

public class TopTen implements Comparator<TopTen> { 

    .... 
    .... 

    @Override 
    public int compareTo(TopTen other) { 

     if (this == other) return EQUAL; 

     return this.getPhonenumber().compareToIgnoreCase(other.getPhonenumber()); 

    } 

這將比較基礎上,phonenumber領域的兩項TOPTEN對象。如果您希望根據另一條件對對象進行排序,請使用該對象返回-1(之前),0(等於)或1(之後)。

例如,要立足排序上incoming,使用以下命令:

@Override 
public int compareTo(TopTen other) { 

    final int BEFORE = -1; 
    final int EQUAL = 0; 
    final int AFTER = 1; 

    if (this == other) return 0; 

    if (this.getIncoming() > other.getIncoming()) { 
     return AFTER; 
    } else if (this.getIncoming() < other.getIncoming()) { 
     return BEFORE; 
    } else { 
     return EQUAL; 
    } 

} 

這將讓你按升序incoming字段值有序TOPTEN對象。

0

?嘗試在TopTen類中實現Comparable接口並覆蓋compareTo方法來指定您的分類邏輯

@Override 
public int compareTo(TopTen o) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

(或)

Collections.sort(SortList, new Comparator<TopTen>(){ 
      public int compare(TopTen t1, TopTen t2) { 
       return t1.phonenumber.compareTo(t2.phonenumber); 
      } 
     }); 
相關問題