View Single Post
  #2 (permalink)  
Old February 11th, 2007, 09:49 PM
ciderpunx ciderpunx is offline
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Here's an example of how to do it by name. Date is trickier cf. http://forum.java.sun.com/thread.jsp...sageID=1850175

Code:
import java.util.*;   
import java.io.File;

public class FileSort {
  public static void main (String argv[]) {
    System.out.println("Sorted list of files example");
    File cwd = new File(".");        // current working directory
    String[] files = cwd.list();     // array of Strings of filenames
    Arrays.sort(files, String.CASE_INSENSITIVE_ORDER); // sorts them
    for (int i=0;i<files.length;i++) {
      System.out.println(files[i]);  // prints them
    }
  }

}
Gives me:

Code:
charlie@mogadon:~/filesort$ ls -1r
random
FileSort.java
FileSort.class
aaa
charlie@mogadon:~/filesort$ javac FileSort.java
charlie@mogadon:~/filesort$ java FileSort
Sorted list of files example
aaa
FileSort.class
FileSort.java
random
charlie@mogadon:~/filesort$
HTH,
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk