【发布时间】:2014-08-26 08:49:04
【问题描述】:
在将String 的List 转换为Integer List 之后,我正在尝试对Integer 的List 进行排序。它给了我这个错误:
没有找到适合排序的方法(List,StringToInteger.CustomComparator)
这是我的代码:
public class StringToInteger {
public static void main(String args) {
List<String> strList = new ArrayList<String>();
strList.add("34");
strList.add("14");
strList.add("42");
strList.add("24");
List<String> resultList = getIntegerArray(strList);
System.out.println("before sorting"+resultList);
Collections.sort(resultList, new CustomComparator());
System.out.println("after sorting"+resultList);
}
private static List<Integer> getIntegerArray(List<String> stringArray) {
List<Integer> result = new ArrayList<Integer>();
for(String stringValue : stringArray) {
try {
//Convert String to Integer, and store it into integer array list.
result.add(Integer.parseInt(stringValue));
} catch(NumberFormatException nfe) {
//System.out.println("Could not parse " + nfe);
Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
}
}
return result;
}
class CustomComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> o1, List<Integer> o2) {
return o1.get(1).compareTo(o2.get(1));
}
}
}
【问题讨论】:
-
我正在使用 Collections.sort(resultList, new CustomComparator()); 对列表进行排序,它在代码中
标签: java sorting collections arraylist