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

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

/* ------------------------------------------------------------ */
/* File entry struct                                             */
/* ------------------------------------------------------------ */
typedef struct {
    char name[32];      /* file name without extension */
    char extension[8];  /* extension including dot */
    Date date;          /* real date struct */
    int size;           /* bytes */
} FileEntry;

/* ------------------------------------------------------------ */
/* Helper: print a date in YYYY-MM-DD format                     */
/* ------------------------------------------------------------ */
void formatDate(const Date* d, char* buffer, size_t bufferSize) {
    snprintf(buffer, bufferSize, "%04d-%02d-%02d", d->year, d->month, d->day);
}

/* ------------------------------------------------------------ */
/* Sort by name (lexicographically)                              */
/* ------------------------------------------------------------ */
int compareByName(const void* a, const void* b) {
    const FileEntry* fa = (const FileEntry*)a;
    const FileEntry* fb = (const FileEntry*)b;
    
    return strcmp(fa->name, fb->name);
}

void sortByName(FileEntry* files, size_t count) {
    qsort(files, count, sizeof(FileEntry), compareByName);
}

/* ------------------------------------------------------------ */
/* Sort by extension (lexicographically)                         */
/* ------------------------------------------------------------ */
int compareByExtension(const void* a, const void* b) {
    const FileEntry* fa = (const FileEntry*)a;
    const FileEntry* fb = (const FileEntry*)b;
    
    return strcmp(fa->extension, fb->extension);
}

void sortByExtension(FileEntry* files, size_t count) {
    qsort(files, count, sizeof(FileEntry), compareByExtension);
}

/* ------------------------------------------------------------ */
/* Sort by date (year → month → day)                             */
/* ------------------------------------------------------------ */
int compareByDate(const void* a, const void* b) {
    const FileEntry* fa = (const FileEntry*)a;
    const FileEntry* fb = (const FileEntry*)b;

    if (fa->date.year != fb->date.year)
        return (fa->date.year < fb->date.year) ? -1 : 1;
    if (fa->date.month != fb->date.month)
        return (fa->date.month < fb->date.month) ? -1 : 1;
    if (fa->date.day != fb->date.day)
        return (fa->date.day < fb->date.day) ? -1 : 1;
        
    return 0;
}

void sortByDate(FileEntry* files, size_t count) {
    qsort(files, count, sizeof(FileEntry), compareByDate);
}

/* ------------------------------------------------------------ */
/* Sort by size (ascending)                                      */
/* ------------------------------------------------------------ */
int compareBySize(const void* a, const void* b) {
    const FileEntry* fa = (const FileEntry*)a;
    const FileEntry* fb = (const FileEntry*)b;

    if (fa->size < fb->size) return -1;
    if (fa->size > fb->size) return 1;
    
    return 0;
}

void sortBySize(FileEntry* files, size_t count) {
    qsort(files, count, sizeof(FileEntry), compareBySize);
}

/* ------------------------------------------------------------ */
/* Helper: print file list                                       */
/* ------------------------------------------------------------ */
void printFiles(const FileEntry* files, size_t count) {
    char dateBuf[16];
    size_t i;
    for (i = 0; i < count; ++i) {
        formatDate(&files[i].date, dateBuf, sizeof(dateBuf));
        printf("%s%s  %s  %d bytes\n",
               files[i].name,
               files[i].extension,
               dateBuf,
               files[i].size);
    }
    printf("\n");
}

int main(void) {
    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}
    };
    size_t count = sizeof(files) / sizeof(files[0]);

    printf("Original:\n");
    printFiles(files, count);

    printf("Sorted by name:\n");
    sortByName(files, count);
    printFiles(files, count);

    printf("Sorted by extension:\n");
    sortByExtension(files, count);
    printFiles(files, count);

    printf("Sorted by date:\n");
    sortByDate(files, count);
    printFiles(files, count);

    printf("Sorted by size:\n");
    sortBySize(files, count);
    printFiles(files, count);

    return 0;
}



/*
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 12 hours ago by avibootz

Related questions

...