DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Sort An Array Of File By Modification Date Descending

08.08.2005
| 25763 views |
  • submit to reddit
        
File f = new File(path);

File [] files = f.listFiles();

Arrays.sort( files, new Comparator()
{
	public int compare(Object o1, Object o2) {
	
		if (((File)o1).lastModified() > ((File)o2).lastModified()) {
			return -1;
		} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
			return +1;
		} else {
			return 0;
		}
	}

}); 
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 1:13pm

Thank you a lot llucifer this looks more nice !!

Snippets Manager replied on Mon, 2012/05/07 - 1:13pm

or even new Long(((File)o1).lastModified()).compareTo(new Long(((File) o2).lastModified()); or with 1.5 autoboxing ((File)o1).lastModified().compareTo((File)o2).lastModified())