Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用集合对对象进行排序。sort()。出错_Android_List_Sorting_Collections - Fatal编程技术网

Android 使用集合对对象进行排序。sort()。出错

Android 使用集合对对象进行排序。sort()。出错,android,list,sorting,collections,Android,List,Sorting,Collections,我正在尝试使用Collections.sort()对java中的对象列表进行排序。但我一直遇到这样的错误:类型参数不在其范围内”。有人知道我如何解决这个问题吗 我的代码 public List<String> fetchNumbersForLeastContacted() { List<String> phonenumberList = getUniquePhonenumbers(); List<TopTen> SortList

我正在尝试使用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类中实现
Comparable
接口,并重写
compareTo
方法以指定排序逻辑

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

Collections.sort(SortList,newcomparator(){
公共整数比较(前十名t1、前十名t2){
返回t1.phonenumber.compareTo(t2.phonenumber);
}
});
这将根据
phonenumber
字段比较前十名的两个对象。如果希望根据其他条件对对象进行排序,请使用该条件返回-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;
    }

}
@覆盖
公共国际比较(前十名其他){
之前的最终整数=-1;
最终整数等于0;
之后的最终整数=1;
如果(this==other)返回0;
if(this.getIncoming()>other.getIncoming()){
返回后;
}else if(this.getIncoming()
这将使您按升序
传入的
字段值排列前十名对象

Collections.sort(SortList, new Comparator<TopTen>(){
            public int compare(TopTen t1, TopTen t2) {
                return t1.phonenumber.compareTo(t2.phonenumber);
            }
        });
public static void sort (List<T> list)
public class TopTen  implements Comparator<TopTen> {

    ....
    ....

    @Override
    public int compareTo(TopTen other) {

        if (this == other) return EQUAL;

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

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

}