Click here to Skip to main content
14,932,329 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey Guys,

I am looking for a code in which I can sort files and folders (specifically files) in a directory based on their date and name. Like suppose i want to sort files based on their names starting with "S" first. After that I want to sort those sorted files once again based on their date. So that every day I can get the files of the same date.

Till now i have sorted files based on their names. But how to sort files based on their date, that i cant understand. Can you guys help me with a sample code?
I am attaching my code in the below section.

What I have tried:

package count;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Newcount2 {

public static void main(String[] args) {
List<string> files = new ArrayList<>();
List<string> directories = new ArrayList<>();
List<string> smallScount = new ArrayList<>();
List<string> capScount = new ArrayList<>();

File folder = new File("The directory Path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files.add(listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
directories.add(listOfFiles[i].getName());
}
}

//dividing the files with caps S and small S.

System.out.println("Files with s and S.");
for (String file : files) {
if(file.startsWith("s"))
smallScount.add(file);
if(file.startsWith("S"))
capScount.add(file);
}

/* System.out.println("List of files :\n---------------");
for(String fName: files) System.out.println(fName);

System.out.println("\nList of directories :\n---------------------");
for(String dName: directories) System.out.println(dName);*/

/* System.out.println("List of files with small 'S' :\n---------------");
for(String fName: smallScount) System.out.println(fName);*/
/*
System.out.println("List of files with Capital 'S' :\n---------------");
for(String fName: capScount) System.out.println(fName);*/

System.out.println("Count of files with small 'S' :\n---------------");
System.out.println(smallScount.size());

System.out.println("Count of files with Capital 'S' :\n---------------");
System.out.println(capScount.size());

}
}
Posted
Updated 6-Aug-18 8:48am

1 solution

Use Arrays.sort() (see Arrays (Java Platform SE 7 )[^]) with a Comparator (Java Platform SE 7 )[^].

You can perform the sort on your orginal listOfFiles array or use a copy if that must be preserved.

Untested example which does not differentiate between files and folders:
Java
Arrays.sort(listOfFiles, new Comparator<File>(){
    public int compare(File f1, File f2)
    { 
        // Names beginning with 's' on top
        boolean s1 = f1.getName().startsWith("s") || f1.getName().startsWith("S");
        boolean s2 = f2.getName().startsWith("s") || f2.getName().startsWith("S");
        if (s1 && !s2)
            return -1;
        if (s2 && !s1)
            return -1;
        // Everything else including both names beginning with 's' is sorted by date
        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
    } });
   
Comments
Member 13939515 6-Aug-18 14:49pm
   
Hi Jochen,
Thanks for your answer. But one part is not clear, you have used f1.lastModified(), is it the same as creation date? Can I Get creation date using java code in the same requirement???
Jochen Arndt 7-Aug-18 2:25am
   
To get the creation date you have to get the file attributes. Search the web for something like "java file creation date".

But note that file creation dates are not supported by some file systems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900