Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java 1.4 Sort Files – Recent & Latest

I’m having Java 1.4 version and need to sort the files by last modified. Sometimes I need to do it by recent and sometimes do it by oldest. I am not sure since Java 1.4 older version can be used in that project

File directory = new File("c:\\books\\");
File[] files = directory.listFiles();

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For java 1.2 or higher:

    File directory = new File("c:\\books\\");
    File[] files = directory.listFiles();
    Arrays.sort(files, new Comparator() {
        public int compare(Object o1, Object o2) {
            File a1=(File)o1;
            File a2=(File)o2;
            if (a1.lastModified() < a2.lastModified())
                return -1;
            else if (a1.lastModified() > a2.lastModified())
                return 1;
            else
                return 0;
        }
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading