Use of Comparator interface Java - Sort files

﹥>﹥吖頭↗ 提交于 2019-12-12 01:48:54

问题


I have a program to sort files in a certain directory of the computer. I am using the Comparator-interface and using the collections.sort-method but I cannot access the output - from the calling-class. I neither know how to sort the objects in the Sort-class either.

1) Would be glad if someone could tell how I use the compare-method (prototyp is: sort(List list, Comparator c)

2) How do I get the output in the directory class? Because Sort-class is parameterisized I cannot access the method public String getName()

class Directory that creates object of class Sort and put those in an Arraylist (member of Sort)

 private Sort sort = new Sort();
 file = new File(System.getProperty(dir));
 File[] files = getFiles(); // return only files, not directories;

 for (int i = 0; i < files.length; i++) {
    sort.arrayList.add(new Sort(files[i])); // put those in an ArrayList belonging to sort
 }

list(sort); // call list-method



public void list(Comparator<File> sortOrder) {
    Collections.sort(sort.arrayList, sortOrder);

// now - how do I get the sorted fileNames from here?
}

the Sort-class

public class Sort<File> implements Comparator<Sort<File>> {

private File file;
public ArrayList <Sort> arrayList = new ArrayList <Sort> ();


public Sort(File file) {
    this.file = file;
}

public Sort() {

}

public String getName() {
    return this.file.toString();
}

// callback-method. Used when calling Collections.sort() in the other class.
public int compare(Sort<File> n1, Sort<File> n2){
 // how do I sort objects on Filesnames.
}    

回答1:


First things first, if you want the Comparator to compare File then tell it that:

public class Sort implements Comparator<File> {

    @Override
    public int compare(File n1, File n2){
        return n1.getName().compareTo(n2.getName);
    }    

}

You are asking it to compare instances of itself. And the declaration Sort<File> tells the compiler that you want a generic class where the generic type parameter happens to be called File. This has nothing to do with the File class.

In order to use this Comparator all you need to do is:

final File file = new File(System.getProperty(dir));
final File[] files = file.listFiles();
Arrays.sort(files, new Sort());
for(final File f : files) {
    //do something with f
}

Or better yet, simply use an anonymous class, this will prevent you from doing anything odd with the Comparator:

Arrays.sort(files, new Comparator<File>() {
    @Override
    public int compare(File o1, File o2) {
        return o1.getName().compareTo(o2.getName());
    }
});

But, if you are using Java 8, you can skip this mess entirely:

final Path path = Paths.get(System.getProperty(dir));
final List<Path> files = new ArrayList<>();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
    stream.forEach(files::add);
}
files.sort(Comparator.comparing(Path::getFileName));

Now you have a sorted List<Path>, you can do whatever you want with it. For example to print the sorted list to the console:

files.forEach(System.out::println);



回答2:


You simply need to loop through the values and perform whatever the required tasks are on those values.

So in your example:

public void list(Comparator<File> sortOrder) {
    Collections.sort(sort.arrayList, sortOrder);

// now - how do I get the sorted fileNames from here?
   for (Sort<File> arrayList : file) {
     System.out.println("Sorted list filenames: " + file.getName());
     /* ... other actions ... */
   }

}

It should be that simple provided you have implemented the correct generic comparator methods



来源:https://stackoverflow.com/questions/28644484/use-of-comparator-interface-java-sort-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!