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

2 Answers

0 votes
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

struct FileEntry {
    std::string name;      // file name without extension
    std::string extension; // extension including dot
    std::string date;      // YYYY-MM-DD
    int size;              // bytes
};

// ------------------------------------------------------------
// Sort by name (lexicographically)
// ------------------------------------------------------------
void sortByName(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.name < b.name;
              });
}

// ------------------------------------------------------------
// Sort by extension (lexicographically)
// ------------------------------------------------------------
void sortByExtension(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.extension < b.extension;
              });
}

// ------------------------------------------------------------
// Sort by date (string YYYY-MM-DD sorts correctly)
// ------------------------------------------------------------
void sortByDate(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.date < b.date;
              });
}

// ------------------------------------------------------------
// Sort by size (ascending)
// ------------------------------------------------------------
void sortBySize(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.size < b.size;
              });
}

// ------------------------------------------------------------
// Print file list
// ------------------------------------------------------------
void printFiles(const std::vector<FileEntry>& files) {
    for (const auto& f : files) {
        std::cout << f.name << f.extension << "  "
                  << f.date << "  "
                  << f.size << " bytes\n";
    }
    std::cout << "\n";
}

int main() {
    std::vector<FileEntry> files = {
        {"G1zTo5jJk", ".jpg", "2007-07-08", 51954},
        {"LTEE4SI0j", ".jpg", "2011-11-13", 43442},
        {"PDqmuO3GH", ".cpp", "2004-05-21", 3346},
        {"qJO2qjukZ", ".png", "2010-08-27", 67087},
        {"HqclTqxb4", ".cpp", "2020-09-05", 70531},
        {"imVyTyoaF", ".jpg", "2011-03-19", 43846},
        {"rXwXdy8XO", ".txt", "2017-10-12", 70193},
        {"9Z4fbOBUc", ".pdf", "2004-06-09", 1754},
        {"ZHahuu4vS", ".txt", "2003-10-10", 65126},
        {"0SnZHh2GT", ".png", "2006-10-18", 25890}
    };

    std::cout << "Original:\n";
    printFiles(files);

    std::cout << "Sorted by name:\n";
    sortByName(files);
    printFiles(files);

    std::cout << "Sorted by extension:\n";
    sortByExtension(files);
    printFiles(files);

    std::cout << "Sorted by date:\n";
    sortByDate(files);
    printFiles(files);

    std::cout << "Sorted by size:\n";
    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 15 hours ago by avibootz
0 votes
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

// ------------------------------------------------------------
// Date struct for proper date handling
// ------------------------------------------------------------
struct Date {
    int year;
    int month;
    int day;
};

// ------------------------------------------------------------
// File entry struct
// ------------------------------------------------------------
struct FileEntry {
    std::string name;      // file name without extension
    std::string extension; // extension including dot
    Date date;             // real date struct
    int size;              // bytes
};

// ------------------------------------------------------------
// Helper: print a date in YYYY-MM-DD format
// ------------------------------------------------------------
std::string formatDate(const Date& d) {
    char buffer[11];
    std::snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d", d.year, d.month, d.day);
    return buffer;
}

// ------------------------------------------------------------
// Sort by name (lexicographically)
// ------------------------------------------------------------
void sortByName(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.name < b.name;
              });
}

// ------------------------------------------------------------
// Sort by extension (lexicographically)
// ------------------------------------------------------------
void sortByExtension(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.extension < b.extension;
              });
}

// ------------------------------------------------------------
// Sort by date (year → month → day)
// ------------------------------------------------------------
void sortByDate(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return std::tie(a.date.year, a.date.month, a.date.day) <
                         std::tie(b.date.year, b.date.month, b.date.day);
              });
}

// ------------------------------------------------------------
// Sort by size (ascending)
// ------------------------------------------------------------
void sortBySize(std::vector<FileEntry>& files) {
    std::sort(files.begin(), files.end(),
              [](const FileEntry& a, const FileEntry& b) {
                  return a.size < b.size;
              });
}

// ------------------------------------------------------------
// Helper: print file list
// ------------------------------------------------------------
void printFiles(const std::vector<FileEntry>& files) {
    for (const auto& f : files) {
        std::cout << f.name << f.extension << "  "
                  << formatDate(f.date) << "  "
                  << f.size << " bytes\n";
    }
    std::cout << "\n";
}

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

    std::cout << "Original:\n";
    printFiles(files);

    std::cout << "Sorted by name:\n";
    sortByName(files);
    printFiles(files);

    std::cout << "Sorted by extension:\n";
    sortByExtension(files);
    printFiles(files);

    std::cout << "Sorted by date:\n";
    sortByDate(files);
    printFiles(files);

    std::cout << "Sorted by size:\n";
    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 15 hours ago by avibootz
...