How to sort a list of files by name, extension, dates, and size in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class FileSorter {

    /** ------------------------------------------------------------ */
    /** Date struct for proper date handling                         */
    /** ------------------------------------------------------------ */
    static class Date {
        int year;
        int month;
        int day;

        Date(int y, int m, int d) {
            this.year = y;
            this.month = m;
            this.day = d;
        }

        /** ------------------------------------------------------------ */
        /** Print a date in YYYY-MM-DD format                            */
        /** ------------------------------------------------------------ */
        String format() {
            return String.format("%04d-%02d-%02d", year, month, day);
        }
    }

    /** ------------------------------------------------------------ */
    /** File entry struct                                            */
    /** ------------------------------------------------------------ */
    static class FileEntry {
        String name;       // file name without extension
        String extension;  // extension including dot
        Date date;         // real date struct
        int size;          // bytes

        FileEntry(String name, String ext, Date date, int size) {
            this.name = name;
            this.extension = ext;
            this.date = date;
            this.size = size;
        }
    }

    /** ------------------------------------------------------------ */
    /** Sort by name (lexicographically)                             */
    /** ------------------------------------------------------------ */
    static void sortByName(List<FileEntry> files) {
        files.sort(Comparator.comparing(f -> f.name));
    }

    /** ------------------------------------------------------------ */
    /** Sort by extension (lexicographically)                        */
    /** ------------------------------------------------------------ */
    static void sortByExtension(List<FileEntry> files) {
        files.sort(Comparator.comparing(f -> f.extension));
    }

    /** ------------------------------------------------------------ */
    /** Sort by date (year → month → day)                           */
    /** ------------------------------------------------------------ */
    static void sortByDate(List<FileEntry> files) {
        files.sort(Comparator
                .comparing((FileEntry f) -> f.date.year)
                .thenComparing(f -> f.date.month)
                .thenComparing(f -> f.date.day));
    }

    /** ------------------------------------------------------------ */
    /** Sort by size (ascending)                                     */
    /** ------------------------------------------------------------ */
    static void sortBySize(List<FileEntry> files) {
        files.sort(Comparator.comparingInt(f -> f.size));
    }

    /** ------------------------------------------------------------ */
    /** Print file list                                      */
    /** ------------------------------------------------------------ */
    static void printFiles(List<FileEntry> files) {
        for (FileEntry f : files) {
            System.out.println(f.name + f.extension + "  "
                    + f.date.format() + "  "
                    + f.size + " bytes");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        List<FileEntry> files = new ArrayList<>();

        files.add(new FileEntry("G1zTo5jJk", ".jpg", new Date(2007,7,8), 51954));
        files.add(new FileEntry("LTEE4SI0j", ".jpg", new Date(2011,11,13), 43442));
        files.add(new FileEntry("PDqmuO3GH", ".cpp", new Date(2004,5,21), 3346));
        files.add(new FileEntry("qJO2qjukZ", ".png", new Date(2010,8,27), 67087));
        files.add(new FileEntry("HqclTqxb4", ".cpp", new Date(2020,9,5), 70531));
        files.add(new FileEntry("imVyTyoaF", ".jpg", new Date(2011,3,19), 43846));
        files.add(new FileEntry("rXwXdy8XO", ".txt", new Date(2017,10,12), 70193));
        files.add(new FileEntry("9Z4fbOBUc", ".pdf", new Date(2004,6,9), 1754));
        files.add(new FileEntry("ZHahuu4vS", ".txt", new Date(2003,10,10), 65126));
        files.add(new FileEntry("0SnZHh2GT", ".png", new Date(2006,10,18), 25890));

        System.out.println("Original:");
        printFiles(files);

        System.out.println("Sorted by name:");
        sortByName(files);
        printFiles(files);

        System.out.println("Sorted by extension:");
        sortByExtension(files);
        printFiles(files);

        System.out.println("Sorted by date:");
        sortByDate(files);
        printFiles(files);

        System.out.println("Sorted by size:");
        sortBySize(files);
        printFiles(files);
    }
}



/*
run:

Original:
G1zTo5jJk.jpg  2007-07-08  51954 bytes
LTEE4SI0j.jpg  2011-11-13  43442 bytes
PDqmuO3GH.cpp  2004-05-21  3346 bytes
qJO2qjukZ.png  2010-08-27  67087 bytes
HqclTqxb4.cpp  2020-09-05  70531 bytes
imVyTyoaF.jpg  2011-03-19  43846 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes
9Z4fbOBUc.pdf  2004-06-09  1754 bytes
ZHahuu4vS.txt  2003-10-10  65126 bytes
0SnZHh2GT.png  2006-10-18  25890 bytes

Sorted by name:
0SnZHh2GT.png  2006-10-18  25890 bytes
9Z4fbOBUc.pdf  2004-06-09  1754 bytes
G1zTo5jJk.jpg  2007-07-08  51954 bytes
HqclTqxb4.cpp  2020-09-05  70531 bytes
LTEE4SI0j.jpg  2011-11-13  43442 bytes
PDqmuO3GH.cpp  2004-05-21  3346 bytes
ZHahuu4vS.txt  2003-10-10  65126 bytes
imVyTyoaF.jpg  2011-03-19  43846 bytes
qJO2qjukZ.png  2010-08-27  67087 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes

Sorted by extension:
HqclTqxb4.cpp  2020-09-05  70531 bytes
PDqmuO3GH.cpp  2004-05-21  3346 bytes
G1zTo5jJk.jpg  2007-07-08  51954 bytes
LTEE4SI0j.jpg  2011-11-13  43442 bytes
imVyTyoaF.jpg  2011-03-19  43846 bytes
9Z4fbOBUc.pdf  2004-06-09  1754 bytes
0SnZHh2GT.png  2006-10-18  25890 bytes
qJO2qjukZ.png  2010-08-27  67087 bytes
ZHahuu4vS.txt  2003-10-10  65126 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes

Sorted by date:
ZHahuu4vS.txt  2003-10-10  65126 bytes
PDqmuO3GH.cpp  2004-05-21  3346 bytes
9Z4fbOBUc.pdf  2004-06-09  1754 bytes
0SnZHh2GT.png  2006-10-18  25890 bytes
G1zTo5jJk.jpg  2007-07-08  51954 bytes
qJO2qjukZ.png  2010-08-27  67087 bytes
imVyTyoaF.jpg  2011-03-19  43846 bytes
LTEE4SI0j.jpg  2011-11-13  43442 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes
HqclTqxb4.cpp  2020-09-05  70531 bytes

Sorted by size:
9Z4fbOBUc.pdf  2004-06-09  1754 bytes
PDqmuO3GH.cpp  2004-05-21  3346 bytes
0SnZHh2GT.png  2006-10-18  25890 bytes
LTEE4SI0j.jpg  2011-11-13  43442 bytes
imVyTyoaF.jpg  2011-03-19  43846 bytes
G1zTo5jJk.jpg  2007-07-08  51954 bytes
ZHahuu4vS.txt  2003-10-10  65126 bytes
qJO2qjukZ.png  2010-08-27  67087 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes
HqclTqxb4.cpp  2020-09-05  70531 bytes

*/

 



answered 22 hours ago by avibootz
edited 22 hours ago by avibootz

Related questions

...