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

1 Answer

0 votes
// ------------------------------------------------------------
// Date struct for proper date handling
// ------------------------------------------------------------
function makeDate(year, month, day) {
    return { year, month, day };
}

// ------------------------------------------------------------
// print a date in YYYY-MM-DD format
// ------------------------------------------------------------
function formatDate(d) {
    return `${d.year.toString().padStart(4, "0")}-${d.month
        .toString()
        .padStart(2, "0")}-${d.day.toString().padStart(2, "0")}`;
}

// ------------------------------------------------------------
// File entry struct
// ------------------------------------------------------------
function makeFile(name, extension, date, size) {
    return {
        name,          // file name without extension
        extension,     // extension including dot
        date,          // real date struct
        size           // bytes
    };
}

// ------------------------------------------------------------
// Sort by name (lexicographically)
// ------------------------------------------------------------
function sortByName(files) {
    return [...files].sort((a, b) => a.name.localeCompare(b.name));
}

// ------------------------------------------------------------
// Sort by extension (lexicographically)
// ------------------------------------------------------------
function sortByExtension(files) {
    return [...files].sort((a, b) => a.extension.localeCompare(b.extension));
}

// ------------------------------------------------------------
// Sort by date (year → month → day)
// ------------------------------------------------------------
function sortByDate(files) {
    return [...files].sort((a, b) => {
        if (a.date.year !== b.date.year) return a.date.year - b.date.year;
        if (a.date.month !== b.date.month) return a.date.month - b.date.month;
        return a.date.day - b.date.day;
    });
}

// ------------------------------------------------------------
// Sort by size (ascending)
// ------------------------------------------------------------
function sortBySize(files) {
    return [...files].sort((a, b) => a.size - b.size);
}

// ------------------------------------------------------------
// print file list
// ------------------------------------------------------------
function printFiles(files) {
    files.forEach(f => {
        console.log(`${f.name}${f.extension}  ${formatDate(f.date)}  ${f.size} bytes`);
    });
    console.log();
}

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
const files = [
    makeFile("G1zTo5jJk", ".jpg", makeDate(2007,7,8), 51954),
    makeFile("LTEE4SI0j", ".jpg", makeDate(2011,11,13), 43442),
    makeFile("PDqmuO3GH", ".cpp", makeDate(2004,5,21), 3346),
    makeFile("qJO2qjukZ", ".png", makeDate(2010,8,27), 67087),
    makeFile("HqclTqxb4", ".cpp", makeDate(2020,9,5), 70531),
    makeFile("imVyTyoaF", ".jpg", makeDate(2011,3,19), 43846),
    makeFile("rXwXdy8XO", ".txt", makeDate(2017,10,12), 70193),
    makeFile("9Z4fbOBUc", ".pdf", makeDate(2004,6,9), 1754),
    makeFile("ZHahuu4vS", ".txt", makeDate(2003,10,10), 65126),
    makeFile("0SnZHh2GT", ".png", makeDate(2006,10,18), 25890)
];

console.log("Original:");
printFiles(files);

console.log("Sorted by name:");
printFiles(sortByName(files));

console.log("Sorted by extension:");
printFiles(sortByExtension(files));

console.log("Sorted by date:");
printFiles(sortByDate(files));

console.log("Sorted by size:");
printFiles(sortBySize(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
imVyTyoaF.jpg  2011-03-19  43846 bytes
LTEE4SI0j.jpg  2011-11-13  43442 bytes
PDqmuO3GH.cpp  2004-05-21  3346 bytes
qJO2qjukZ.png  2010-08-27  67087 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes
ZHahuu4vS.txt  2003-10-10  65126 bytes

Sorted by extension:
PDqmuO3GH.cpp  2004-05-21  3346 bytes
HqclTqxb4.cpp  2020-09-05  70531 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
qJO2qjukZ.png  2010-08-27  67087 bytes
0SnZHh2GT.png  2006-10-18  25890 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes
ZHahuu4vS.txt  2003-10-10  65126 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 16 hours ago by avibootz

Related questions

...