-1
我想按其中包含的File
的大小对ArrayList
进行排序。ArrayList <File>按file.length排序()
public class ArraySorting implements Comparator<File> {
public ArraySorting() { }
public ArrayList<File> sort(ArrayList<File> arrayOfFiles) {
Arrays.sort(arrayOfFiles, new ArraySorting());
return arrayOfFiles;
}
public int compare(File a, File b) {
long aSize = a.length();
long bSize = b.length();
if (aSize < bSize) {
return -1;
}
else if (aSize > bSize) {
return 1;
}
else {
return 0;
}
}
}
我得到了一个错误:
The method sort(T[], Comparator) in the type Arrays is not applicable for the arguments (ArrayList, ArraySorting).
你使用Collections.sort(...)吗? – nafas 2014-11-05 12:46:26
'ArrayList'是一个列表,而不是一个数组!使用'Collections.sort(arrayOfFiles,new ArraySorting())'。 – Seelenvirtuose 2014-11-05 12:46:33
您是否尝试使用Google搜索错误? http://stackoverflow.com/questions/12889314/comparator-wont-work-with-arrays-sort – 2014-11-05 12:46:40