How to filter and order a set of files?

I need to filter an ArrayList of Files by png images, and I need to order it from oldest to most recent, but it would be nice if it ordered not by last modified but by creation date.

This is the relevant code (variable names and methods in Spanish, sorry):

AdminImagen class

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

class AdminImagen {
  ArrayList<File> archivos;
  ArrayList<Imagen> imagenes;

  AdminImagen(String ruta) {
    archivos=listarArchivos(ruta);
    imagenes=prepararImagenes(archivos);
  }

  ArrayList<File>listarArchivos(String ruta) {
    File file=new File(ruta);

    ArrayList<File> lista = new ArrayList<File>();
    if (file.isDirectory()) {
      File[] arch=file.listFiles();
      for (int i=0;i<arch.length;i++) {
        lista.add(arch[i]);
      }
      return lista;
    } 
    else return null;
  }

  ArrayList<Imagen> prepararImagenes(ArrayList<File> arch) {
    ArrayList<Imagen> im=new ArrayList<Imagen>();
    for (File f:arch) {
      String ruta=f.getAbsolutePath();
      Date fecha=new Date(f.lastModified());
      Imagen imagen=new Imagen(ruta, fecha);
      im.add(imagen);
    }
    return im;
  }

  String getRutaImagen(int a) {
    return imagenes.get(a).rutaArchivo;
  }

  Date getFechaImagen(int a) {
    return imagenes.get(a).hora;
  }

  String getHoraImagen(int a) {
    Date fecha = getFechaImagen(a);
    String hora = fecha.getHours()+":"+fecha.getMinutes();
    return hora;
  }
}

Imagen class

import java.io.File;
import java.util.Date;

class Imagen {
  String rutaArchivo;
  Date hora;

  Imagen(String ruta, Date fecha) {
    rutaArchivo=ruta;
    hora=fecha;
  }

  Imagen() {
    rutaArchivo="";
    hora=null;
  }

  //crear un objeto Imagen a partir de un File
  void extraerInfoImagen(File archivo) {
    rutaArchivo=archivo.getAbsolutePath();
    hora=new Date(archivo.lastModified());
  }
}

I really don't know how to make these operations, so could you help me out here? Thanks

Answers

  • edited April 2017 Answer ✓

    As you mighta seen from http://forum.processing.org/one/topic/listing-last-10-modified-files-in-directory,
    we gotta use a FilenameFilter reference as an argument for listFiles() method! :-\"

    Here are some relevant parts from my posted example there:


    import java.io.FilenameFilter;
    
    static final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] EXTS = {
        "png", "jpg", "jpeg", "gif", "tif", "tiff", "tga"
      };
    
      @ Override final boolean accept(final File dir, String name) {
        name = name.toLowerCase();
        for (final String ext: EXTS)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    

    protected static final File[] getFolderContent(final File dir) {
      return dir.listFiles(PIC_FILTER);
    }
    

  • edited April 2017 Answer ✓

    Now for the sorting part, we're gonna need both File[] + Comparator references as arguments for Arrays.sort() method. =:)

    There, I've chosen to use a Map<File, Long> to temporarily store each File w/ its corresponding lastModified() value. :ar!

    Again, some relevant snippets from my full code:


    import java.util.Arrays;
    import java.util.Comparator;
    
    static final Comparator<File> modifiedComparator = new Comparator<File>() {
      @ Override final int compare(final File f1, final File f2) {
        return Long.signum(modifiedCache.get(f1) - modifiedCache.get(f2)); //safe!
        //return 1 | (int) (modifiedCache.get(f1) - modifiedCache.get(f2) >> 63); //risky!
      }
    };
    

    import java.util.Map;
    import java.util.WeakHashMap;
    
    static final Map<File, Long> modifiedCache = new WeakHashMap<File, Long>(0200);
    

    protected static final void fillModifiedCache(final File[] archives) {
      for (final File f : archives)  modifiedCache.put(f, f.lastModified());
    }
    

    // ...
    
      fillModifiedCache(files);
      Arrays.sort(files, modifiedComparator);
    
    // ...
    

  • Your code was spot on, had to remove the final static parts so that it would work in my particular case, but the rest was used as is. Thanks a lot, I'll credit you in my proyect :)

  • edited May 2014

    sorry for double post.

Sign In or Register to comment.