You should not rely on listFiles () for an ordered list of pages:
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#listFiles ()
"There is no guarantee that the name strings in the resulting array will be displayed in any particular order:"
You need to create your own ordering system based on the file name or lastModified or file size. You can use the Comparator <File> or Comparator <String> to sort files in a SortedSet, or, as mentioned earlier, create your own class for sorting elements that implements Comparable. I would suggest the first solution, since it's a little silly to wrap a File or String class in another for this function.
An example with a large amount of memory overhead:
TreeSet<File> pages = new TreeSet<File>(new Comparator<File>(){ public int compare(File first, File second) { return first.getName().compareTo(second.getName()); } }); for (File file : allFiles) { pages.add(file()); } allFiles = pages.toArray();
If you want a more efficient one, you must implement your own method to sort the array in its place.
source share