Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I was going through JBAKE code at

https://github.com/jbake-org/jbake/blob/master/src/main/java/org/jbake/app/Asset.java : 58

PFB the code.

Why are we sorting the array here?

    if (assets != null) {
        //TBD : Why is sorting required?
        Arrays.sort(assets);
        for (int i = 0; i < assets.length; i++) {
            if (assets[i].isFile()) {
                StringBuilder sb = new StringBuilder();
                sb.append("Copying [" + assets[i].getPath() + "]...");
                File sourceFile = assets[i];
                File destFile = new File(sourceFile.getPath().replace(source.getPath()+File.separator+config.getString(ConfigUtil.Keys.ASSET_FOLDER), destination.getPath()));
                try {
                    FileUtils.copyFile(sourceFile, destFile);
                    sb.append("done!");
                    LOGGER.info(sb.toString());
                } catch (IOException e) {
                    sb.append("failed!");
                    LOGGER.error(sb.toString(), e);
                    e.printStackTrace();
                    errors.add(e.getMessage());
                }
            }

            if (assets[i].isDirectory()) {
                copy(assets[i]);
            }
        }
    }
share|improve this question

1 Answer 1

up vote 2 down vote accepted

The comparator of File performs a lexicographic comparison of pathnames. So, these files are sorted by name. So, obviously, the author wants the filesystem tree to be traversed in alphabetical order, and not in any other order. That's important, because according to the documentation of File.listFiles():

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Java specifically refrains from giving such a guarantee because different host environments (i.e. Windows vs. Linux) are known to yield files in different orders, and sometimes even the host environment may refrain from giving any such guarantee. On the other hand, sorting the files can be expensive, so the creators of Java decided to pass this uncertainty to the programmer, rather than imposing the extra overhead of sorting on every single java application out there.

So, the author wants to have a guaranteed order in which the files will be traversed. This is useful:

  • When debugging, because the behavior of your program does not change from debug run to debug run due to factors that are beyond your control. If you notice that file "aardvark.txt" was yielded first, and you try to see what happens when processing aardvark, and you set a breakpoint at some place and rerun, it is very annoying to discover that on the next run "zoology.txt" is processed first.

  • When testing, because you can create a temporary directory structure, create a tree of files in it, run your function on it, obtain the results in a new tree, and then make sure that the trees are exactly equal, without having to account for the fact that the trees may be equal but not-in-the-same-order.

  • In general, when writing software that you want to behave in a consistent way across different platforms.

share|improve this answer
2  
Also, since you appear to be interested in best practices, you might find this question and its answer to be of interest: stackoverflow.com/q/11227809/773113 (This is probably the most legendary of all stackoverflow questions and answers ever.) – Mike Nakis Dec 6 at 14:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.