我在按名称排序文件时遇到了一些问题。
Number of records: 2048
出现在Number of records: 512
File Set 10.txt:
来自File Set 1.txt:
当我运行我的代码时,我得到了这个输出:
Number of Records: 1024
File Set 1.txt: NOT Sorted: Sorted: 0.003441570093855262
File Set 10.txt: NOT Sorted: Sorted: 0.0017387439729645848
File Set 2.txt: NOT Sorted: Sorted: 7.515490287914872E-4
File Set 3.txt: NOT Sorted: Sorted: 0.0017166410107165575
File Set 4.txt: NOT Sorted: Sorted: 2.553839876782149E-4
File Set 5.txt: NOT Sorted: Sorted: 2.577530103735626E-4
File Set 6.txt: NOT Sorted: Sorted: 2.59725988144055E-4
File Set 7.txt: NOT Sorted: Sorted: 2.3920100647956133E-4
File Set 8.txt: NOT Sorted: Sorted: 2.344639942748472E-4
File Set 9.txt: NOT Sorted: Sorted: 2.6880399673245847E-4
Number of Records: 2048
File Set 1.txt: NOT Sorted: Sorted: 0.0010665359441190958
File Set 10.txt: NOT Sorted: Sorted: 0.0010333789978176355
File Set 2.txt: NOT Sorted: Sorted: 0.001001800992526114
File Set 3.txt: NOT Sorted: Sorted: 0.001030615996569395
File Set 4.txt: NOT Sorted: Sorted: 0.0010112749878317118
File Set 5.txt: NOT Sorted: Sorted: 9.946960490196943E-4
File Set 6.txt: NOT Sorted: Sorted: 0.0010148270521312952
File Set 7.txt: NOT Sorted: Sorted: 9.958799928426743E-4
File Set 8.txt: NOT Sorted: Sorted: 9.587769745849073E-4
File Set 9.txt: NOT Sorted: Sorted: 6.394480005837977E-4
Number of Records: 512
File Set 1.txt: NOT Sorted: Sorted: 3.828799890470691E-5
File Set 10.txt: NOT Sorted: Sorted: 3.868299972964451E-5
File Set 2.txt: NOT Sorted: Sorted: 3.868299972964451E-5
File Set 3.txt: NOT Sorted: Sorted: 3.828799890470691E-5
File Set 4.txt: NOT Sorted: Sorted: 3.789300171774812E-5
File Set 5.txt: NOT Sorted: Sorted: 3.828799890470691E-5
File Set 6.txt: NOT Sorted: Sorted: 3.710400051204488E-5
File Set 7.txt: NOT Sorted: Sorted: 6.433900125557557E-5
File Set 8.txt: NOT Sorted: Sorted: 6.236600165721029E-5
File Set 9.txt: NOT Sorted: Sorted: 6.473500252468511E-5
它假设是:
Number of Records: 512
File Set 1.txt: NOT Sorted: Sorted: 0.003441570093855262
File Set 2.txt: NOT Sorted: Sorted: 7.515490287914872E-4
File Set 3.txt: NOT Sorted: Sorted: 0.0017166410107165575
File Set 4.txt: NOT Sorted: Sorted: 2.553839876782149E-4
File Set 5.txt: NOT Sorted: Sorted: 2.577530103735626E-4
File Set 6.txt: NOT Sorted: Sorted: 2.59725988144055E-4
File Set 7.txt: NOT Sorted: Sorted: 2.3920100647956133E-4
File Set 8.txt: NOT Sorted: Sorted: 2.344639942748472E-4
File Set 9.txt: NOT Sorted: Sorted: 2.6880399673245847E-4
File Set 10.txt: NOT Sorted: Sorted: 0.0017387439729645848
Number of Records: 1024
File Set 1.txt: NOT Sorted: Sorted: 0.0010665359441190958
File Set 2.txt: NOT Sorted: Sorted: 0.001001800992526114
File Set 3.txt: NOT Sorted: Sorted: 0.001030615996569395
File Set 4.txt: NOT Sorted: Sorted: 0.0010112749878317118
File Set 5.txt: NOT Sorted: Sorted: 9.946960490196943E-4
File Set 6.txt: NOT Sorted: Sorted: 0.0010148270521312952
File Set 7.txt: NOT Sorted: Sorted: 9.958799928426743E-4
File Set 8.txt: NOT Sorted: Sorted: 9.587769745849073E-4
File Set 9.txt: NOT Sorted: Sorted: 6.394480005837977E-4
File Set 10.txt: NOT Sorted: Sorted: 0.0010333789978176355
Number of Records: 2048
File Set 1.txt: NOT Sorted: Sorted: 3.828799890470691E-5
File Set 2.txt: NOT Sorted: Sorted: 3.868299972964451E-5
File Set 3.txt: NOT Sorted: Sorted: 3.828799890470691E-5
File Set 4.txt: NOT Sorted: Sorted: 3.789300171774812E-5
File Set 5.txt: NOT Sorted: Sorted: 3.828799890470691E-5
File Set 6.txt: NOT Sorted: Sorted: 3.710400051204488E-5
File Set 7.txt: NOT Sorted: Sorted: 6.433900125557557E-5
File Set 8.txt: NOT Sorted: Sorted: 6.236600165721029E-5
File Set 9.txt: NOT Sorted: Sorted: 6.473500252468511E-5
File Set 10.txt: NOT Sorted: Sorted: 3.868299972964451E-5
这是我的代码:
boolean runTest(String[] text, int[] number, String url, String out, Sort sort) {
PrintWriter filename;
boolean tobeReturned = true;
String beforeSorting = "";
String afterSorting = "";
long startTime;
double timeTaken;
try{
filename = createWriter(out);
File dir = new File(url);
File[] listDir = dir.listFiles();
Arrays.sort(listDir);
for(File directory : listDir){
File[] listOfFiles = directory.listFiles();
Arrays.sort(listOfFiles);
filename.println("Number of Records: \t" + directory.getName());
for (File file : listOfFiles) {
//println(file.getName());
text = loadStrings(file);
number = int(text);
if(isSorted(number)){beforeSorting = "Sorted";}else{beforeSorting = "NOT Sorted";};
startTime = startTime();
sort.sortInteger(number);
timeTaken = stopTime(startTime);
if(isSorted(number)){afterSorting = "Sorted";}else{afterSorting = "NOT Sorted";};
//filename.println("File Set " + file.getName() + ": \t\t" + stopTime(startTime()));
filename.println("File Set " + file.getName() + ": \t\t" + beforeSorting + ": \t" + afterSorting + ": \t" + timeTaken);
timeTaken = 0;
}
filename.println("\n");
}
filename.flush();
filename.close();
} catch (Exception e) {
tobeReturned = false;
}
return tobeReturned;
}
答案 0 :(得分:3)
您的文件按字典顺序排序。如果要按数字顺序对它们进行排序,则必须提供自定义比较器,您应该在其中解析文件名,提取数字部分并按此数字部分进行比较
get
答案 1 :(得分:1)
您需要实现自己的比较器,根据整数部分比较文件名。
Arrays.sort(listDir, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
int num1 = Integer.parseInt(o1.getName().split("\\.")[0]);
int num2 = Integer.parseInt(o2.getName().split("\\.")[0]);
return num1 - num2;
}
});
答案 2 :(得分:0)