jeudi 30 août 2012
Java - ordering listFiles()
/**
* return files list ordered by type (directory/file) and by name:
* step 1 : first the directories and after the files (type sorting).
* step 2 : after this type sorting, we order by name (alphabetical order).
* @param file Directory to list
* @return
*/
private File[] listFiles(File file) {
File[] files = file.listFiles();
Arrays.sort(files, new Comparator<File>() {
//return positive value if f1 is previous to f2
//return negative value if f2 is previous to f1
public int compare(final File f1, final File f2) {
boolean sameType = (f1.isDirectory() == f2.isDirectory());
if (sameType) {
//same type, then only compare by name
//if nameCompare<0 then f1 is previous to f2 (alphabetical order)
return f1.getName().compareTo(f2.getName());
} else {
if (f1.isDirectory()) {
//f1 is a directory, then f2 is a file, and then f1 is previous to f2 (we must return negative value)
return -1;
} else {
//f2 is a directory, then f1 is a file, and then f2 is previous to f1 (we must return positive value)
return 1;
}
}
}
});
return files;
}
Inscription à :
Publier les commentaires (Atom)
Categories
- /etc/hosts (1)
 - 443 (1)
 - 80 (1)
 - a2dp (1)
 - addsubview (1)
 - affix (1)
 - amazon (1)
 - android (6)
 - angularjs (1)
 - angularjs cross domain json post (1)
 - angularjs ionic ng-click twice (1)
 - animate (1)
 - ansible (2)
 - antlr (1)
 - apache2 processes (1)
 - app (1)
 - app store (1)
 - apple (1)
 - appstore (1)
 - avis (2)
 - baignoire (1)
 - basics (1)
 - bitbucket (1)
 - blackberry (1)
 - bluetooth (1)
 - booster (1)
 - bootstrap (3)
 - bottle (4)
 - browsers (1)
 - buffer (1)
 - cherrypy (3)
 - chromebook real life (1)
 - coder (2)
 - communicator (1)
 - config.txt (1)
 - creme chocolat (1)
 - crepes bretonnes (1)
 - crlf (1)
 - css (2)
 - cuisine (1)
 - database (4)
 - datatables (1)
 - datetime (1)
 - delete (1)
 - disconnect (1)
 - distributing (1)
 - english (2)
 - flask (1)
 - fontawesome (1)
 - francais (51)
 - futuristic (1)
 - game (1)
 - gil (1)
 - git (9)
 - github (1)
 - gratuit (2)
 - hadopi (1)
 - header (1)
 - height zero (1)
 - hibernate (1)
 - hotel (2)
 - http (1)
 - https (2)
 - ionic (2)
 - ios (2)
 - ios7 (1)
 - iOS8 (1)
 - iphone (1)
 - jaune (1)
 - java (7)
 - javamelody (1)
 - javascript (1)
 - json (3)
 - kindle (1)
 - knockout (3)
 - leaflet (1)
 - legere (1)
 - lf (1)
 - life cycle (1)
 - linkedin resume builder profile (1)
 - linux (9)
 - log (1)
 - luxembourg (1)
 - machine (1)
 - maizena (1)
 - minecraft (1)
 - mobile (1)
 - mongo (1)
 - mongodb (3)
 - mongodb mongo linux (1)
 - multiple (1)
 - myspace (1)
 - mysql (1)
 - netbeans cordova android cordova-plugin-file (1)
 - nginx (2)
 - nintendo (1)
 - number (1)
 - opensolaris (1)
 - openstreetmap (1)
 - oracle (15)
 - order by (1)
 - output (1)
 - overclocking (1)
 - packaging (1)
 - parameters (1)
 - personnaliser bootstrap (1)
 - photos (1)
 - pip (2)
 - pipewire (1)
 - prime (1)
 - problem (2)
 - project (1)
 - publish (1)
 - python (26)
 - raspberry pi (2)
 - raspi-config (1)
 - recette (1)
 - redirect (1)
 - restful (2)
 - schema (1)
 - screen (1)
 - screencast (1)
 - script (1)
 - scrollspy (1)
 - serialization (1)
 - sessions (2)
 - shell (1)
 - shutdown reboot linux reinstall apt-get (1)
 - smartgwt (1)
 - sncf (1)
 - software (1)
 - spring (1)
 - sql (1)
 - sticky footer (1)
 - supervisor (1)
 - systeme (1)
 - tile (1)
 - timestamp (1)
 - title (1)
 - tornado (3)
 - turbo (1)
 - ubuntu (2)
 - uialertview (1)
 - video capture (1)
 - viewdidappear (1)
 - viewdidload (1)
 - viewwillappear (1)
 - weasyprint (1)
 - web (1)
 - web2py (1)
 - windows (5)
 - worker (1)
 - xbox wireless headset (1)
 - xdk barcode scanner intel.xdk.device.barcode.scan (1)
 - XDK rename project (1)
 - xeno galaxies (1)
 
Aucun commentaire:
Enregistrer un commentaire