p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Java 2 (http://p2p.wrox.com/forumdisplay.php?f=30)
-   -   Sort the files in folder (http://p2p.wrox.com/showthread.php?t=35284)

abhisheksud November 21st, 2005 03:59 AM

Sort the files in folder
 
I am making a webpage which shows all the file in a folder and make links to the file. However i want to know how i can sort the view of files according to file name or date created of by size(any one). I am using the for loop statement for displaying all the files.what modification should i do so that all the files are shown alphabetically in acending order...Also the file size is shown in bytes as a default. How can i make these appear in mb or kb to decimal digits....Pl help me out... the code which i am using is given below

for each file in files %>
<tr>
<td style="width: 164px"><br />
<a href="patches/<%=file.name%>"><%=file.name%></a></td>
<td style="width: 196px" align=center><br /><%=file.size %> Kb</td>
<td style="width: 230px" align=center><br /><%=file.DateLastModified %>
</td></tr>

ciderpunx February 11th, 2007 09:49 PM

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


All times are GMT -4. The time now is 12:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.