We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How do I implement LastModifiedFileComparator
Page Index Toggle Pages: 1
How do I implement LastModifiedFileComparator (Read 273 times)
How do I implement LastModifiedFileComparator
Nov 29th, 2008, 9:12pm
 
Hi there I am trying to sort files in date order.
This seems to be the java command but I don't know how to implement it in processing. Can someone help?

http://commons.apache.org/io/apidocs/org/apache/commons/io/comparator/LastModifiedFileComparator.html


thanks
Christian
Re: How do I implement LastModifiedFileComparator
Reply #1 - Nov 29th, 2008, 9:43pm
 
This is to demonstrate how a comparator is built and put to work. Try adding these test files to your data folder (use import), then try modifying one of the files:



import java.io.File;
import java.util.ArrayList;


class LastModifiedFileComparator implements Comparator{
public int compare(Object o1, Object o2) {
               Long i = ((File)o1).lastModified() -  ((File)o2).lastModified();
return i.intValue();
}
}
File f1 = new File(dataPath("hello.txt"));
File f2 = new File(dataPath("world.txt"));
File f3 = new File(dataPath("cliche.txt"));

ArrayList files = new ArrayList();

files.add(f1);
files.add(f2);
files.add(f3);


for(int i = 0; i < files.size(); i++){
 File x = (File)files.get(i);
 println(x.getName());
println("" + x.lastModified());
}

Collections.sort(files, new LastModifiedFileComparator() );

for(int i = 0; i < files.size(); i++){
 File x = (File)files.get(i);
 println(x.getName());
println("" + x.lastModified());
}

Re: How do I implement LastModifiedFileComparator
Reply #2 - Nov 29th, 2008, 10:28pm
 
thanks but I don't understand how to use this for existing files from a folder that i want to load in date order

If I make a 'new File' as I load the files then after sorting I get the files in the order I loaded them in rather than date modified order...

sorry confused

Re: How do I implement LastModifiedFileComparator
Reply #3 - Nov 29th, 2008, 10:44pm
 
I can see how it is confusing.

A File object is a *representation* of a file, so when you create a File object like:

File f = new File(  <some path> );

You really aren't creating a new file.  In English, you are saying "I would like to create a representative object of a file in this location (indicated by file path name, like "/User/user1/sometext.txt").  If I want to do anything to this file, I will refer to this representation."


That's why you need to actually import a file into the "Data" directory of your sketch before you can manipulate it.



So, if you want to sort a file that already exist elsewhere, try replacing:  

File f1 = new File(dataPath("hello.txt"));


with

File f1 = new File("  ....some path name .... ");


Where some path name is the file path of your file.

For an explanation of how file paths work, see:
http://en.wikipedia.org/wiki/Path_(computing)
Page Index Toggle Pages: 1