2015-10-27 65 views
3

我在按文件名排序文件时遇到了一些问题。按名称排序文件返回未排序的数组

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 

这是我的鳕鱼e:

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

如果按字典顺序排序,“10”在“1”之后和“2”之前。如果你不想这样做,写一个适合你的比较器。 – khelwood

回答

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

您的文件按辞典顺序排列。如果你想将它们按数字顺序进行排序,你必须提供自定义的比较,你应该分析文件名,提取数字部分由该数字部分

Arrays.sort(listOfFiles, new Comparator<String>() { 

     Integer getInt(String fileName) { 
      //Implement number extraction 
     } 

     @Override 
     public int compare(String fileNameA, String fileNameB) { 
      return getInt(fileNameA).compareTo(getInt(fileNameA)); 
     } 
    });