iKing
iKing

Reputation: 697

Sort file based on date using Java

I need to sort files with respect to their date . I have successfully written code which sorts the files based on last modification but help me to sort files with respect to their date. Older files should come first.

File dir = new File("E:\\myfiles");
File[] files = dir.listFiles();
Arrays.sort(files, new Comparator<File>() {
    public int compare(File f1, File f2)
    {
        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
    }
});

for (int i = 0; i < files.length; i++)
    System.out.println(files[i]);

my files are

E:\myfiles\test_20140704.csv
E:\myfiles\test_20140705.csv
E:\myfiles\test_20140703.csv
E:\myfiles\test_20140706.csv

Upvotes: 1

Views: 531

Answers (3)

Eric Nicolas
Eric Nicolas

Reputation: 1547

If you want something a little bit more robust than just sorting by name (which should indeed work for the sample filenames you provided), you should parse the date in the filename and sort by this date. For instance:

public int compare(File f1, File f2)
{
    Date d1 = fileNameToDate(f1);
    Date d2 = fileNameToDate(f2);
    return d1.compareTo(d2);
}

//caution: this static is better for performance, but may not be thread-safe
private static SimpleDateFormat DF_YYYYMMDD = new SimpleDateFormat("yyyyMMdd");

private Date fileNameToDate(File f)
{
   int pos = f.getName().lastIndexOf('_');
   if (pos < 0)
   {
     //TODO: error handling if filename is not in the correct format
   }
   String dateStr = f.getName().substring(pos + 1, pos + 1 + 8);
   try
   {
     return DF_YYYYMMDD.parse(dateStr);
   }
   catch(ParseException e)
   {
     //TODO: error handling if filename is not in the correct format        
   }
}

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

try this

    String[] files = dir.list();
    Arrays.sort(files, new Comparator<String>() {
        public int compare(String f1, String f2) {
            return f1.compareTo(f2);
        }
    });

Upvotes: 0

vinayknl
vinayknl

Reputation: 1252

If you are just looking to sort by Name of the file ( based on syntax provided in your question). Just changing the compare method would do.

File dir = new File("E:\\myfiles");
File[] files = dir.listFiles();
Arrays.sort(files, new Comparator<File>() {
   public int compare(File f1, File f2)
   {
      //return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
      return f1.getName().compareTo(f2.getName());
   }
   });

(Below is not relevant as per your latest comment)

File class does not support getting creation time. But Java 7 has a feature which can help

BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());

See http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

Upvotes: 1

Related Questions