File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how sort a array of files and directories.. ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how sort a array of files and directories.. ?" Watch "how sort a array of files and directories.. ?" New topic
Author

how sort a array of files and directories.. ?

Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 87
import java.util.*;
.
.
.
.
File[] filesInDir = directory.listFiles();
Arrays.sort(filesInDir);


but not work... for me, i need all directories, first... and then the files or viceverza... no problem, here the results (not like me):


Dir Name:C:\Documents and Settings\Miguel Enriquez\Favorites\assembler
Nombre Favorito: Art of Assembly Language Programming and HLA by Randall Hyde

Dir Name:C:\Documents and Settings\Miguel Enriquez\Favorites\assembler
Nombre Favorito: Art of Assembly Language Programming and HLA by Randall Hyde2

Dir Name:C:\Documents and Settings\Miguel Enriquez\Favorites
Nombre Favorito: Batanga - Latin Music Internet Radio

Dir Name:C:\Documents and Settings\Miguel Enriquez\Favorites\Becas
Nombre Favorito: CONACYT- Convocatoria de Demanda Libre 2004 - DAFCyT

see Batanga.... because Batanga are first than Becas.... but i want list
sorted, first the directries, and then my files....

any advice?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Then you need a Comparator to enforce that sequence:
Raed Qudaisat
Greenhorn

Joined: Dec 28, 2005
Posts: 7
use filters ,,,
************
File dir=new File(File_Path+File_Name);
File[] list;
FileFilter filter=new FileFilter(){
public boolean accept(File file) {
return file.isDirectory();
}
};

list=dir.listFiles(filter);
************
this will get you all directories,,,,

replace "return file.isDirectory();" with "return file.isFile();" to list the files instead
Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 87
Paul i try compile my program with the comparator and get these error:

C:\programa\java\Mysql>javac listfi.java
listfi.java:84: cannot find symbol
symbol : method compareTo(java.lang.Object)
location: class java.lang.Object
return o1.compareTo(o2);
^
Note: listfi.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


thanks....
Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 87
i modified the comparator: (worked!!!)
NOTE: display first the directoryes and then the files, how sort in inverse mode? first files and then directories?

thanks

Comparator comp = new Comparator()
{
public int compare(Object o1, Object o2)
{
File f1 = (File) o1;
File f2 = (File) o2;
if (f1.isDirectory() && !f2.isDirectory())
{
// Directory before non-directory
return -1;
}
else if (!f1.isDirectory() && f2.isDirectory())
{
// Non-directory after directory
return 1;
}
else
{
// Alphabetic order otherwise
//return o1.compareTo(o2);
return f1.compareTo(f2);
}
}
};
Arrays.sort(filesInDir, comp);
Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 87
i changed

if (f1.isDirectory() && !f2.isDirectory())
{
// Directory before non-directory
return 1;
}
else if (!f1.isDirectory() && f2.isDirectory())
{
// Non-directory after directory
return -1;
}


for first sort the filenames and then directories.

Thanks...
much thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how sort a array of files and directories.. ?
 
Similar Threads
how sort a array of files and directories.. ?
Mysql JDBC Cant load.....
Who came First?....Compiler Or Language?
i Explain more....
Systems level difference between osx linux and unix