使用Collections.sort()对对象进行排序。得到一个错误

时间:2013-09-28 05:46:58

标签: android list sorting collections

我正在尝试使用Collections.sort()对java中的对象列表进行排序。但我一直收到这个错误:类型参数不在其范围内。。有谁知道如何解决这个问题?

我的代码

   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 个答案:

答案 0 :(得分:2)

public static void sort (List<T> list)

此方法只能在T填充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对象。

答案 1 :(得分: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);
            }
        });