android - 使用 Collections.sort() 对对象进行排序。出现错误

标签 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);

十大类

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;
}}

最佳答案

public static void sort (List<T> list)

此方法仅在 T 实现 Comparable 接口(interface)时才能使用。 实现 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(之后)。

例如,要根据传入进行排序,请使用以下命令:

@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;
    }

}

这将使您获得按传入字段值升序排序的前十个对象。

关于android - 使用 Collections.sort() 对对象进行排序。出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063757/

相关文章:

android - DataBaseHelper,是否单例?

android - adb 的 USB 驱动程序不适用于摩托罗拉 mc40

java - 如何从android中的一个Arraylist中获取单独的数据项值

python - 按字典值对字典列表进行排序

python - 如何对文件中的特定信息进行排序

jquery 数据表插件似乎无法正确对带有链接的列进行排序

android - 无法从命令行执行sdk管理器命令: Android

python - 包含 python 列表的类的多个实例

javascript - 从没有数组的函数链中提取数据

python - 在 Python Pandas 中以长格式追加列表元素