【问题标题】:Sorting Files Array by LastModefiedTime按 LastModefiedTime 对文件数组进行排序
【发布时间】:2016-03-01 16:55:35
【问题描述】:

我想按 lastModefiedTime 从最旧到最新对文件数组进行排序(第一个元素必须是最旧的,最后一个元素是最新的)。

我写了这个方法,但是没有用!!

private static void swap(File[] files,int a, int b){
    File h = files[a];
    files[a]=files[b];
    files[b]=h;
}

public static void fillBoxList(String path){



    File dir = new File(path);
    File[] files = dir.listFiles();

    if (files != null) {

        //Box.addToFilesArray(directoryListing[0]);
// print the List before Sorting 
        for(int i =0;i<dfiles.length;i++){
            Log.i("LastModDate", new Date(directoryListing[i].lastModified())+"");
        }
//Beginn of Sorting
        for (int i =1;i<files.length;i++) {

            Date lastModdate1 = new Date(files[i-1].lastModified());
            Date lastModDate2 = new Date(files[i].lastModified());

                 if(lastModdate1.compareTo(lastModDate2)>1){
                     swap(files,i-1,i);
                 }
        }
// print the List after Sorting
        for(int i =0;i<directoryListing.length;i++){
            Log.i("SortedLastModDate", new Date(directoryListing[i].lastModified())+"");
        }
    } else {

        Log.e("DircError","directory dont exists");
    }
}

你能告诉我我做错了什么吗?

【问题讨论】:

标签: java android algorithm sorting


【解决方案1】:

我建议你先检查比较语句返回什么值

int comparison = date.compareTo(date2);

int 比较2 = date2.compareTo(date); int compare3 = date.compareTo(date);

记录这些 int 变量,然后检查如何排序,或者可能是您的日期格式不正确

【讨论】:

    【解决方案2】:

    您的排序算法实际上是错误的。 Bubble sort 中应该有两个循环

    while (true){
        boolean swapped = false;
        for (int i =1;i<files.length;i++) {
            Date lastModdate1 = new Date(files[i-1].lastModified());
            Date lastModDate2 = new Date(files[i].lastModified());
            if(lastModdate1.compareTo(lastModDate2)>1){
                  swap(files,i-1,i);
                  swapped = true;
            }
        }
        if ( !swapped )
          break;
    }
    

    【讨论】:

    • 我试过你的解决方案。当我使用 lastModdate1.compareTo(lastModDate2)>1 进行操作时,我得到了最新的作为第一个元素。所以我把它改成 lastModdate1.compareTo(lastModDate2)
    • lastModdate1.compareTo(lastModDate2)&lt;1 如果列表中有两个元素相等,将导致无限循环。使用:lastModdate1.compareTo(lastModDate2)&lt;0
    • 在这种情况下我该怎么办?
    • 使用这个:lastModdate1.compareTo(lastModDate2)&lt;0
    • 从最旧到最新对 List 进行排序的正确方法是 lastModdate1.compareTo(lastModDate2)>0。我的错误是我与 1 比较!谢谢你,我的朋友,你拯救了我的一天!
    【解决方案3】:

    您可以使用这种 Java 8 方法:

    public static void main(String[] args) throws IOException {
        Stream<File> sortedFiles = getSortedFiles("src/main/resources");
        sortedFiles.forEach(f ->
            System.out.printf("File: %s, last modified %s \n", f.getName(), new Date(f.lastModified()))
        );
    }
    
    public static Stream<File> getSortedFiles(String path) throws IOException {
        List<File> files = new ArrayList<>();
        try (Stream<Path> list = Files.list(Paths.get(path))) {
            list.map(Path::toFile)
                    .sorted(Comparator.comparing(File::lastModified))
                    .forEach(files::add);
        }
        return files.stream();
    }
    

    【讨论】:

    • 我们的项目使用 java 7
    【解决方案4】:

    你可以写一个java.util.Comparator 来比较LastModefiedTime,然后用java.utils.Arrays.sort(T[] a, Comparator&lt;? super T&gt; c)) 对你的数组进行排序。 您不需要自己实现排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多