In some circumstance it is neccessary to get a sorted List of files from folder. Therefore the Java API provides some functions to access the local file system. The following snippets will demonstrate how the identified files can be sorted useful:
- Sort files by their lastModified date
- Sort files by their name
- Sort files by their Id on the end of the file name
Sort files by their lastModified date
The first code show the sorting for files by their lastModified date, whereby the sorting (ASC/DESC) can be controlled via flag.
package it.heber.sandbox; import java.io.File; import java.util.Arrays; import java.util.Comparator; import java.util.Date; public class FileComperator { private static File[] images; public static void main(String[] args) { File folder = new File("/tmp/images/"); images = folder.listFiles(); sortFilesByLastModifiedDate(true); } /** * Function sorts files by their lastModified date * * @param sortAsc sorting Flag [true=ASC|false=DESC] */ public static void sortFilesByLastModifiedDate(final boolean sortAsc) { Comparator<File> comparator = new Comparator<File>() { @Override public int compare(File o1, File o2) { if (sortAsc) { return Long.valueOf(o1.lastModified()). compareTo(o2.lastModified()); } else { return -1 * Long.valueOf(o1.lastModified()). compareTo(o2.lastModified()); } } }; Arrays.sort(images, comparator); for (File image : images) { System.out.println(image.getName() + "t" + new Date(image.lastModified())); } } }
The output is following:
img_26.png Tue Mar 20 21:44:34 CET 2012 img_27.png Tue Mar 20 21:44:36 CET 2012 img_1.png Tue Mar 20 21:44:38 CET 2012 img_2.png Tue Mar 20 21:44:40 CET 2012
Sort files by their name
The second part shows the sorting for files by their name, whereby in this case the alphabetical order is respected. The sorting (ASC/DESC) can be controlled via flag.
package it.heber.sandbox; import java.io.File; import java.util.Arrays; import java.util.Comparator; import java.util.Date; public class FileComperator { private static File[] images; public static void main(String[] args) { File folder = new File("/tmp/images/"); images = folder.listFiles(); sortFilesByName(true); } /** * Function sorts files by their name * * @param sortAsc sorting Flag [true=ASC|false=DESC] */ public static void sortFilesByName(final boolean sortAsc) { Comparator<File> comparator = new Comparator<File>() { @Override public int compare(File o1, File o2) { if (sortAsc) { return (o1.getName()).compareTo(o2.getName()); } else { return -1 * (o1.getName()).compareTo(o2.getName()); } } }; Arrays.sort(images, comparator); System.out.println("###n### Sort file by name ######"); for (File image : images) { System.out.println(image.getName() + "t" + new Date(image.lastModified())); } } }
The output is following:
img_18.png Tue Mar 20 21:44:22 CET 2012 img_19.png Tue Mar 20 21:44:23 CET 2012 img_2.png Tue Mar 20 21:44:40 CET 2012 img_20.png Tue Mar 20 21:44:25 CET 2012 img_21.png Tue Mar 20 21:44:26 CET 2012
Sort files by their Id on the end of the file name
The third part show the sorting for files by an Id, which are on the end of a file name. The sorting (ASC/DESC) can be controlled via flag. Around this function some other helper function are neccessary, to split the Id for sorting from file name.
package it.heber.sandbox; import java.io.File; import java.util.Arrays; import java.util.Comparator; import java.util.Date; public class FileComperator { private static File[] images; public static void main(String[] args) { File folder = new File("/tmp/images/"); images = folder.listFiles(); sortFilesByIdName(true); } /** * Function sorts files by their Id on the end of the file name. * (e.q. img_1.png, img_2.png, ...) * * @param sortAsc sorting Flag [true=ASC|false=DESC] */ public static void sortFilesByIdName(final boolean sortAsc) { Comparator<File> comparator = new Comparator<File>() { @Override public int compare(File o1, File o2) { if (sortAsc) { return getFileId(o1).compareTo(getFileId(o2)); } else { return -1 * getFileId(o1).compareTo(getFileId(o2)); } } }; Arrays.sort(images, comparator); System.out.println("###n### Sort file by Id in file name ######"); for (File image : images) { System.out.println(image.getName() + "t" + new Date(image.lastModified())); } } /** * Helper method to determine file Id for sorting. File name has following * structure: img_11.png * * @param file * @return */ private static Integer getFileId(File file) { String fileName = first(file.getName().split(".")); String fileId = last(fileName.split("_")); return Integer.parseInt(fileId); } /** * Generic helper methode to get the last field of an array * * @param <T> Array type * @param array Array with elements * @return last field of an array */ private static <T> T last(T[] array) { return array[array.length - 1]; } /** * Generic helper methode to get the first field of an array * * @param <T> Array type * @param array Array with elements * @return first field of an array */ private static <T> T first(T[] array) { return array[0]; } }
The output is following:
img_8.png Tue Mar 20 21:44:03 CET 2012 img_9.png Tue Mar 20 21:44:05 CET 2012 img_10.png Tue Mar 20 21:44:07 CET 2012 img_11.png Tue Mar 20 21:44:10 CET 2012 img_12.png Tue Mar 20 21:44:13 CET 2012
Rattling wonderful info can be found on this web site.
Great! This helped a lot! Such a good example that works right away and follow a complete solution from the beginning!
Thumbs up and thanks a lot!
Great solution for sorting files in java by date or name or even parts of the string as an index