I was going through JBAKE code athttps://github.com/jbake-org/jbake/blob/master/src/main/java/org/jbake/app/Asset.java : 58PFB 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]);            }        }    }
Sorting Array before looping : best practice
java;coding style;coding standards;code reviews;clean code
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.